Hi,
I'm using MVC 2 and EF4. If have a view that displays my Application (class) properties. Not all the properties are displayed in the view. There are a couple of the properties that need to be set once the submit button is clicked.
I'm getting client validation to pass, but my server validation is still failing. I receive an Application object in my CreateApplication action, I update a property, and do a ModelState.IsValid check. It is still false. I did a loop through my errors list and it displays the error text that I set on my SubmitterEmployeeNumber property using a Required data annotation. I did set it and I did update my model, but validation is still failing. Here is my code:
[HttpPost]
public ActionResult CreateApplication(Application application)
{
application.SubmitterEmployeeNumber = "123456";
TryUpdateModel(application);
if (ModelState.IsValid)
{
}
}
Here is how I display the view:
public ActionResult CreateApplication()
{
var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);
return View(viewModel);
}
How do I get the validation to pass after I set the property after binding?
What is the difference between UpdateModel and TryUpdateModel and when do I need to use each?
EDIT:
I changed the name of the action to:
[HttpPost]
public ActionResult CreateApp()
{
var application = new Application
{
ApplicationStateID = 1,
SubmitterEmployeeNumber = "123456"
};
if (TryUpdateModel(application))
{
int success = 0;
}
}
Here is my view:
<% using (Html.BeginForm("CreateApp", "Application")) {%>
TryUpdateModel still validates as false. I put in int success = 0; just to see if it will go into it but it doesn't.
Thanks, Brendan