1

in asp.net mvc model i'm retrieving data from database in text box for edit data purpose, but when data retrieved in text box , on right side of text box gives me warning can not be empty.

code in Ado.cs file for retrive data in textbox for update.i come on this page when i click on update record.

public static void UpdateData(int id,MyModel objmydatamodel)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\nisarg parekh\documents\visual studio 2013\Projects\WebApplication5\WebApplication5\App_Data\Database1.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
            con.Open();
            SqlCommand cmd = new SqlCommand("select Username,Password from tb1 where Id="+id, con);
            SqlDataReader dr = cmd.ExecuteReader();

            if(dr.Read())
            {

                objmydatamodel.Username = dr["Username"].ToString();
                objmydatamodel.Password = dr["Password"].ToString();

            }
            else
            {

                dr.Close();
            }

        }
        public static int Updatedata1(MyModel objmymodel,int id)
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\nisarg parekh\documents\visual studio 2013\Projects\WebApplication5\WebApplication5\App_Data\Database1.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
            con.Open();
            SqlCommand cmd = new SqlCommand("update tb1 set Username='" + objmymodel.Username + "',Password='" + objmymodel.Password + "' where Id=" + id, con);
            return cmd.ExecuteNonQuery();


        }

code of controller

 [HttpGet]
        public ActionResult Update(int id, MyModel objmymodel)
        {
            ado.UpdateData(id,objmymodel);
            return View(objmymodel);

        }



        [HttpPost]
        public ActionResult Update(MyModel objmymodel,int id)
        {
           int checkdataupdate= ado.Updatedata1(objmymodel,id);
           if (checkdataupdate > 0)
           {
               return RedirectToAction("Show","Mycontroller");
           }
           else {

               return RedirectToAction("", "can not update");
           }



        }

code in View file

@model WebApplication5.Models.MyModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Update</title>
</head>
<body style="background-color:aliceblue">
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>MyModel</h4>
            <hr />
            @Html.ValidationSummary(true)

            <div class="form-group">
                @Html.LabelFor(model => model.Username, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Username)
                    @Html.ValidationMessageFor(model => model.Username)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Password)
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }


</body>

</html>

code of Model

 public class MyModel
    {

        public int Id { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessage = "Username Required")]
        [Display(Name = "Username")]
        public string Username { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessage = "Password Required")]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }
nisarg parekh
  • 413
  • 4
  • 23
  • sorry i forgot to put my code of Model. it is as under. public class MyModel { public int Id { get; set; } [Required(AllowEmptyStrings = false, ErrorMessage = "Username Required")] [Display(Name = "Username")] public string Username { get; set; } [Required(AllowEmptyStrings = false, ErrorMessage = "Password Required")] [Display(Name = "Password")] public string Password { get; set; } } } – nisarg parekh May 17 '18 at 04:50
  • Because you have a `MyModel objmymodel` parameter in your GET method, so the values you passed in the url for the properties of `MyModel` are added to `ModelState`. Since you did not pass any values, validation fails and you see the error message. Remove the parameter (and initialize it inside the method). –  May 17 '18 at 04:50
  • 1
    And you need to edit your question with the relevant information, not add it in comments –  May 17 '18 at 04:51
  • @Stephen Muecke it's Working. but i could not understand how? can you please explain in detail. – nisarg parekh May 17 '18 at 04:56
  • Refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) to understand the behavior –  May 17 '18 at 04:57
  • @Stephen Muecke Okay Got it. And thanks for your quick reply. – nisarg parekh May 17 '18 at 05:10

0 Answers0