Reputation: 1421
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
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
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