Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If(ModelState.IsValid==false) return View(); or View(model);?

When validation fails, which one I should return? View(); or View(model); ?

I notice both work. It is confusing.

EDIT:

public class MoviesController : Controller
{
    MoviesEntities db = new MoviesEntities();

    //
    // GET: /Movies/

    public ActionResult Index()
    {
        var movies = from m in db.Movies
                     select m;
        return View(movies.ToList());
    }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Movie movie)
    {
        if (ModelState.IsValid)
        {
            db.AddToMovies(movie);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        else
            return View();//View(movie);
    }
}

My Create.aspx:

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            <%: Html.LabelFor(model => model.Title) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Title) %>
            <%: Html.ValidationMessageFor(model => model.Title) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.ReleaseDate) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ReleaseDate) %>
            <%: Html.ValidationMessageFor(model => model.ReleaseDate) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Genre) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Genre) %>
            <%: Html.ValidationMessageFor(model => model.Genre) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Price) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Price) %>
            <%: Html.ValidationMessageFor(model => model.Price) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
like image 830
xport Avatar asked Sep 27 '10 11:09

xport


People also ask

What do you do when a ModelState IsValid is false?

That's because an error exists; ModelState. IsValid is false if any of the properties submitted have any error messages attached to them. What all of this means is that by setting up the validation in this manner, we allow MVC to just work the way it was designed.

How do I remove a ModelState error?

AddModelError() method to add a validationerror to the ModelState. If you then check the IsValid property in the ModelState variable, it will say its not valid. If you call ModelState. Clear() and then check the IsValid property again it will be valid.

What exactly does ModelState IsValid do?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.

What is ModelState IsValid in .NET core?

Model state represents errors that come from two subsystems: model binding and model validation. Errors that originate from model binding are generally data conversion errors. For example, an "x" is entered in an integer field.


1 Answers

If the view you are returning is strongly typed and uses the model it would be better to pass this model. If you simply return View() and in the view you try to access the model you will most probably get a NullReferenceException.

The following is a common pattern:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = FetchModelFromRepo();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SomeViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }        

        // TODO: update db
        return RedirectToAction("index");
    }
}
like image 88
Darin Dimitrov Avatar answered Nov 16 '22 01:11

Darin Dimitrov