Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
Posted
Updated 18-Jul-16 2:22am
v4
Comments
Tiwari Avinash 18-Jul-16 7:25am    
Please be more specific about your error, and provide some more logical codes so that some one can help you.
Vivek.anand34 18-Jul-16 7:32am    
I have updated my question.. actually when click the submit button the page loads and cleared all the fields..
Vivek.anand34 18-Jul-16 7:43am    
s. I found the problem. but solution can u tell me.. I gave ItemCode field is [Required] but values didn't comes to display field thats problem.
Vivek.anand34 18-Jul-16 7:48am    
<p class="itemcode"> @Html.Display("ItemCode") </p>
$('.itemcode').html(data.icode);

this type working.. but showing validation error "Required Item code"
Karthik_Mahalingam 23-Jul-16 5:50am    
do you have team viewer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900