views:

28

answers:

1

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>
+2  A: 

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");
    }
}
Darin Dimitrov
My view is strongly typed. Behind the scene, why both can give me the same result? I am confused.
xport
Maybe because in your view you are only using Html helpers which by default will first look in the POST request and then the model when binding their values. But if you try to access manually a property of the model like `<%: Model.FooProp %>` it will probably throw an exception. So if you have a strongly typed view **always** pass the model.
Darin Dimitrov
Html helpers in scaffold generated by VWD is not strongly typed?
xport
@xport - the HTML helpers are not strongly typed. In this particular case they use reflection against whatever they are passed, to determine how to render it. But ultimately they don't care a bit about your model in particular.
GalacticCowboy
@GalaticCowboy, see my create view, the lambda expression use strongly typed model. So my view is strongly typed?
xport