While developing the MVC applications you need to provide validations that mainly work on the client end.
public class EmployeeModel
{
public int EmpId { get; set; }
[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
[Required(ErrorMessage = "PhoneNo is required")]
public string PhoneNo { get; set; }
public string Address { get; set; }
public getstatus Status { get; set; }
public string msg { get; set; }
}
There is another setting that needs to be checked while performing validations in the Web.config.
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
You need to put the bundle files at the last section of your view .
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
You also have to check whether the code is being checked before the database or before other call being made.
[HttpPost]
public ActionResult RegisterEmployeeDetails(EmployeeModel EmpModel)
{
EmpModObj = new EmployeeModel();
try
{
if (ModelState.IsValid)
{
EmpRepObj = new EmployeeRepository();
EmpRepObj.SaveDetails(EmpModel);
}
else
{
return View("RegisterEmployee");
}
}
catch (Exception ex)
{
EmpModObj.msg = ex.Message;
}
return View(EmpModel);
}
0 Comment(s)