Tips & Trick ASP - NET MVC PDF
Tips & Trick ASP - NET MVC PDF
Tips & Trick ASP - NET MVC PDF
"When should I use a ViewBag vs. ViewData vs. TempData objects?" -- a frequent question in online
forums, during presentations, and at events. There are enough similarities and differences between these
objects that warrant a closer look to see exactly how you can use each of these objects while developing
MVC applications.
All three objects are available as properties of both the view and controller. As a rule of thumb, you'll use
the ViewData, ViewBag, and TempData objects for the purposes of transporting small amounts of data
from and to specific locations (e.g., controller to view or between views). Both the ViewData and ViewBag
objects work well in the following scenarios:
If you need to work with larger amounts of data, reporting data, create dashboards, or work with multiple
disparate sources of data, you can use the more heavy duty ViewModel object.
References: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3applications
In ASP.NET MVC, ViewModels allow you to shape multiple entities from one or more data models or
sources into a single object, optimized for consumption and rendering by the view. The below image
illustrates the concept of a ViewModel:
The purpose of a ViewModel is for the view to have a single object to render, alleviating the need for UI
logic code in the view that would otherwise be necessary. This means the only responsibility, or concern,
of the view is to render that single ViewModel object, aiding in a cleaner separation of concerns (SoC).
Concerns are distinct aspects of the application that have a particular purpose (i.e., concern), and keeping
these aspects apart means your application is more organized, and the code more focused. Putting data
manipulation code in its own location away from the view and controller, enforces SoC.
Using ViewModels in MVC for finer granularity and better SoC leads to more easily maintainable and
testable code. Remember, unit testing is about testing small units.
Along with better coding practices, there are many business reasons demonstrating why you might
consider using ViewModels:
References: http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.netmvc-applications
How to render multiple model into single view page in ASP.NE T MVC? Nice answer from
http://stackoverflow.com/questions/4764011/multiple-models-in-a-view
First opinion:
I'd recommend using Html.RenderAction and PartialViewResults to accomplish this; it will allow
you to display the same data, but each partial view would still have a single view model and removes
the need for a BigViewModel
So your view contain something like the following:
@Html.RenderAction("Login")
@Html.RenderAction("Register")
Where Login & Register are both actions in your controller defined like the following:
public PartialViewResult Login( )
{
return PartialView( "Login", new LoginViewModel() );
}
public PartialViewResult Register( )
{
return PartialView( "Register", new RegisterViewModel() );
}
The Login & Register would then be user controls residing in either the current View folder, or in
the Shared folder and would like something like this:
/Views/Shared/Login.cshtml: (or /Views/MyView/Login.cshtml)
@model LoginViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
/Views/Shared/Register.cshtml: (or /Views/MyView/Register.cshtml)
@model ViewModel.RegisterViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Name)
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
And there you have a single controller action, view and view file for each action with each totally
distinct and not reliant upon one another for anything.
Second opinion: