开发者

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>
    开发者_如何学C    <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>


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");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜