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; }
}