Reputation: 2881
The following are two examples of a controller for submitting data. One returns the input model if validation fails the other doesn't. Can someone enlighten me on which is method correct or preferred? They seem to behave exactly the same.
With returning model
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
//do stuff
}
return View(model);
}
Without returning model
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
//do stuff
}
return View();
}
Upvotes: 1
Views: 9934
Reputation: 4361
Since you have a strongly typed view, in either case you will need to pass the corresponding model to the view (else your view may crash on trying to access properties on a null model). On success you can redirect to another page or you should be able to give user a notification, in the same page itself updating whether the operation is a success or fail (depends on whether you want a usual HTML based solution or an AJAX enabled solution). As for validation there are the options of usual client side validation and the jQuery based unobtrusive client side validation.
Here are couple of useful links
ASP.NET Model validation (this is MVC2 based but should be same for MVC3)
MVC3 unobtrusive client validation
Upvotes: 0
Reputation: 4172
Usually you want the user to show an error message if the data he submitted is not valid (e.g. he did not enter a valid email address into a field that requries to be an email - see MVC ModelValidation for more information).
You use the ModelState.IsValid to check for that. In this case you usually show him the page he came from ("Register") and you also want to show the data he entered into the form fields (= RegisterModel). But you want to display an errormessage that tells him what fields are not correct.
If the user's data is correct and your action succeeds (in this case he was registered successfully) you usually do not show him the registration form again, but you would redirect him to a success page.
Here is a simple example how to validate, show errors and redirect after successfull action.
Example from your code:
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
//do stuff
// redirect to a success page
return RedirectToAction("Success");
}
// data is not valid - show the user his data and error messages
// using Html Helper methods (see link for details)
return View(model);
}
On the Register view (Ifno - if you have a strongly typed view, you can use the HTML Helper overloads for that - e.g. use Html.LabelFor, TextboxFor etc methods):
Show general error message/summary;
...
<%= Html.ValidationSummary("Register was not successfull") %>
Show error message next to the inputs:
...
<%= Html.TextBox("Email") %>
<%= Html.ValidationMessage("Email", "*") %>
Upvotes: 2
Reputation: 11201
The first action at the end is passing the Model to the view and the second one your returning back an empty view. So I would suggest you go for the first one
But when validation fails you should do the error handling this way
if (ModelState.IsValid)
{
//do stuff
}
else
{
ModelState.AddModelError("RegisterModel Errors", "some error occured");
}
return View(model);
Upvotes: 0
Reputation: 46008
Will you view render without model passed to it? Probably not, so the second one will fail.
I would suggest you read about Post Redirect Get pattern
http://en.wikipedia.org/wiki/Post/Redirect/Get
Upvotes: 0