so I tried making an asp.net core 2 (visual studio 2017) . Tried to make a simple view, new, edit, delete for master bank. I have succeeded on the view and create new, but now I'm stuck on the edit part. So, when the user click edit, I open new window and show the detail for them to edit, and then when they click save button, then I saved the values (like when create new). it's just that simple. but for some reason, the modelstate.isvalid for edit always false, and when I tried debugging I found that the items all returned null (they all showed on the view), except for the primary key. pls help where I did wrong :
the class :
public class MsBank { [Required(ErrorMessage = "Required.")] [RegularExpression(@"\b[A-Z0-9]{1,}\b", ErrorMessage = "Must Be Uppercase")] public string BankCode { get; set; } [Required(ErrorMessage = "Required.")] public string BankName { get; set; } public string BankBranch { get; set; } private SqlConnection con; private void connection() { PrjCommon dbhandle = new PrjCommon(); con = new SqlConnection(dbhandle.GetSetting()); } public bool AddBank(List smodel) { connection(); SqlCommand cmd = new SqlCommand("SaveMsBank", con); cmd.CommandType = CommandType.StoredProcedure; foreach (var item in smodel) { cmd.Parameters.AddWithValue("@BankCode", item.BankCode); cmd.Parameters.AddWithValue("@BankName", item.BankName); cmd.Parameters.AddWithValue("@BankBranch", item.BankBranch); cmd.Parameters.AddWithValue("@LastUpdatedBy", "Me"); cmd.Parameters.AddWithValue("@LastUpdatedFromModule", "NET"); } con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); if (i >= 1) return true; else return false; } } }
the view :
@model List[MsBank] @{ ViewData["Title"] = "Edit"; } @using (Html.BeginForm()) { @Html.AntiForgeryToken() @foreach (var item in Model) { Code : @Html.EditorFor(model => item.BankCode, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => item.BankCode, "", new { @class = "text-danger" }) Name : @Html.EditorFor(model => item.BankName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => item.BankName, "", new { @class = "text-danger" }) Branch : @Html.EditorFor(model => item.BankBranch, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => item.BankBranch, "", new { @class = "text-danger" }) } } @ViewBag.Message @Html.ActionLink("Back to List", "Index") @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
and the controller :
public ActionResult Edit(string BankCode) { MsBank ms = new MsBank(); ModelState.Clear(); return View(ms.GetData(BankCode)); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(string BankCode,List smodel) { try { if (ModelState.IsValid) { if (smodel.Count > 0) { MsBank ms = new MsBank(); if (ms.AddBank(smodel)) { ViewBag.Message = "Bank Edited Successfully"; ModelState.Clear(); } return RedirectToAction("Index"); } else { ViewBag.Message = "Empty Record"; } } return View(smodel); } catch (Exception e) { ViewBag.Message = e.ToString(); return View(smodel); } }
so, to make it more clear, modelstate.isvalid above returned false (when debug I found that) because bankname is invalid because it's null, and so bankbranch is null too