Dear Friends,
When validation error occurs all displayFor Values are cleared.. how to resolve this..
What I have tried:
View: @Html.DisplayFor(t => t.RateCode, new { id = "RateCode", @class = "form-control" })
@Html.TextBox("ItemName",null, new { id = "ItemName", @class = "form-control", autocomplete = "off" }) "one More Fields like this"
Controller:
[HttpPost]
public ActionResult Create(ItemRates itemrates)
{
if (ModelState.IsValid)
{
itemrates.GetDate = DateTime.Now;
itemrates.DelFlag = "N";
itemrates.GetUser = Session["LogedUser"].ToString();
itemrates.CompCode = "CMP-001";
db.ItemRates.Add(itemrates);
db.SaveChanges();
ViewBag.Message = "Record Saved Successfully";
ModelState.Clear();
var maxId = (db.ItemRates.Select(x => (int?)x.Id).Max() ?? 0) + 1;
var RC = "IRC-" + maxId;
return View(new ItemRates { RateCode = RC });
}
else { ViewBag.ErrMsg = "Enter Mandatory Fields"; }
return View(itemrates);
}
View: @Html.DisplayFor(t => t.ItemCode, new { id = "ItemCode", @class = "form-control" })
Autocomplete item name:
if (data) {
$('#ItemCode').val(data.icode);
}