LaTeX
LaTeX

Reputation: 1421

What is the difference between passing data via ViewData.Model and View's argument?

I am very new to asp.net mvc. It might be a "childish" question. :-)

We can pass data from within a controller using two methods (among others of course) as follows:

ViewData.Model=obj;
return View();

or

return View(obj);

What is the pros and cons for each approach?

Upvotes: 1

Views: 307

Answers (2)

rob waminal
rob waminal

Reputation: 18419

Both snippets are the same, the first option passed directly to the object model while the second is passed as a parameter to the View but eventually will be passed to the object model.

If you look into the View() method, you can see the first snippet called inside.

protected internal ViewResult View(object model) {
    return View(null, null, model);
}

protected internal virtual ViewResult View(string viewName, string masterName, object model) {
    if (model != null) {
        ViewData.Model = model;
    }

    ....
}

Therefore both are the same but the first is a direct approach.

Upvotes: 3

Boycs
Boycs

Reputation: 5668

Technically I don't think there is any difference...

I prefer to use the second approach as to me it just "feels" nicer...

Upvotes: 1

Related Questions