ViewState vs ViewData in mvc? - asp.net-mvc

What is the difference between viewstate and viewdata in mvc?

I was just going through the MVC framework & the exact question popped up in my head.. I understand the difference as below.
ASP.Net & MVC are two different worlds. But looking closely they are not. The concepts of web remains the same and it just the way to write the code. Ok letz compare them
ASP.Net
.aspx -- So this is the view which contains the html to be rendered in the browser
.aspx.cs -- as we know this is the code behind for doing all the manipulation of the html
So on top of that we have the BO with our properties and which is bound to the controls using databind.
So here comes the ViewState which remembers the data bound to the controls back and forth between the postback.
MVC
View - this holds all the HTML code which in turn is still a .aspx or ascx file
Controller - has the logic behind the HTML. Inside that you have the action methods for performing the specific actions.
So here instead of BO, you have the Models with the same properties which is given to the View to render itself in a different syntax instead of a databind.
Now ViewData is used to bind the anonymous data between the controller and the view.
Comparatively ViewData is more organized and easy to use but apart from that they serve a similar purpose but different in few ways. Like Viewstate is persistent between postbacks and ViewData is not as MVC is stateless.
Hope this explains to an extent

ViewState and ViewData can handle some complex objects.
ViewState is within page lifecycle while ViewData works in very different way. ViewData can be passed to the target view.
Please refer here for understanding of viewState:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
for viewData:
http://www.asp.net/mvc/tutorials/asp-net-mvc-views-overview-cs
hope that helps

View state is used only in ASP.net forms,controls and Page life cycle. View state is used by ASP.net framework to manage control states.
View Data is a dataset or Data which is passed to your View - to dipslay HTML data in MVC,
Viewstate is not used in MVC. Please refer above mentioned links for more details.

Related

Developing ASP.NET MVC web applications the right way

I'm starting to develop a new ASP.NET MVC application, and I'd like to make sure that my understanding about the way of developing applications under MVC pattern is correct.
Question 1: Suppose that I have some main views (MainView1, MainView2, ...) with some partial views in it (PartialView1, PartialView2, PartialView3, ...). From what I have understood about MVC, I should define a Model for each view. e.g. I have to define PartialModel, PartialModel2, ... and also define my main models which are containers of the partial models which have been used in them:
public class MainModel1
{
public PartialModel1 Partial1 {get;set}
public PartialModel2 Partial2 {get;set}
public PartialModel3 Partial3 {get;set}
}
This way when I want to redirect to MainView, I can initialize the models used in that view by using this Model. Is my understanding of MVC correct? Or should communication between Views and Models be in another form in MVC?
Question 2: If the above is correct, Then suppose I have a partial view in some of my main view pages. This partial view has a submit button which calls an action method. This action method should somehow return the main page's view with the right viewmodels for views. Is there any recommendation about how to get/set viewmodels of other views in a partial view's action?
Thanks.
You understanding is correct. Each view (no matter whether it is main or partial) should have a corresponding view model (unless in the very rare case where this view contains only static html of course). And following this logic a main view that has to render other partial views will have a view model which itself will have reference (maybe as properties) to view models that are required by those partial views.
There is another technique which is using the Html.Action helper. In this case the partial is rendered through another controller/action than the main one. So you don't need to reference the partial view model in the main view model. They will be completely distinct. Take a look at the following blog post to learn more about Html.Action.
Your understanding is perfectly fine. This is what you can also refer to ViewModel in Asp.net MVC.
So, What basically is an Asp.net MVC ViewModel ?
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:
Incorporating dropdown lists of lookup data into a related entity
Master-detail records view
Pagination: combining actual data and paging information
Components like a shopping cart or user profile widget
Dashboards, with multiple sources of disparate data
Reports, often with aggregate data

How to do I determine the context of an action in the view (asp.net mvc)

I have a View that contains lots of links, when a user actions one of these links it initiates a filter action on the Controller.
When the View is constructing itself I want to determine which (if any) of the links have been actioned.
I'm overwhelmed with choices, put something in the ViewBag, TempData, or in my ViewModel so that the View can determine the context of the action?
What would be the preferred way?
What would be the preferred way?
ViewModel of course.
ViewBag achieves the same thing as the view model but in a weakly typed manner, so personally I never use it in any of my applications. TempData on the other hand could be considered as a one-redirect-session-storage which I don't see how it would be of any help for your scenario.

How Can I Hide Specific Elements on a Razor View Based on Security without Logic in View?

I have looked all over for elegant solutions to this not so age-old question. How can I lock down form elements within an ASP.Net MVC View, without adding if...then logic all over the place?
Ideally the BaseController, either from OnAuthorization, or OnResultExecultion, would check the rendering form elements and hide/not render them based on role and scope.
Another approach I have considered is writing some sort of custom attributes, so as to stay consistent with how how we lock down ActionResults with [Authorize]. Is this even possible without passing a list of hidden objects to the view and putting if's all over?
Other background info: We will have a database that will tell us at execution time (based on user role/scope) what elements will be hidden. We are using MVC3 with Razor Viewengine. We're utilizing a BaseController where any of the Controller methods can be overridden.
Any help on this would be deeply appreciated!
You could use a number of different methods:
Send the user to a different view (display only view) based on the action filter, or a condition in the controller.
On a field basis, you could build the logic into the editor templates to read custom data-annotations based on role/permission.
You can build HTML helpers to handle the logic and render the appropriate partial view, css class, or text.
For more reading:
How much logic is allowed in ASP.NET
MVC views?
ASP.NET MVC 2 Templates, Part 1: Introduction (there are 5 parts, very informative and a good place to start developing your own editor templates)

Asp.Net MVC UserControl

I'm porting an application from WebForms to MVC.
I have a WebForms UserControl that is used throughout the site and which contains presentational and business logic, and in particular some 'initizliation' logic (in the Page_Load)
My understanding of MVC UerControls is that they have no dedicated controller and thus contain no logic, and the logic is added to the controller of the viewpage to which the UserControl is added.
Now, I don;t want to have to write the initialization logic in my ViewPage, as this UserControl appears on several pages, thus duplication of logic.
I don't think I can user a MasterPage, as the UserControl can be rendered either vertically to the left of the page or hozontally at the top of the page depending on what page it is. This would require 2 MasterPage's, and again duplication of logic.
Do I have any other options?
This is a good case to use the RenderAction html helper. Much better, grab the Microsoft MVC futures and use the RenderAction method. Basically it lets you call a controller action within a view, which is alot more like how UserControls work than RenderPartial.
Bleh, I guess I should do it properly and pass in a object via my controller's viewdata rather than hacky-slash it.

A ASP.NET MVC page with multiple instances of user control consuming the independent data

First let me thank all of you for numerous useful posts I have read during my recent ASP.NET MVC development. My questions is as follows:
I have a ASP.NET MVC application with loosely coupled model, meaning I use a business layer assembly in a controller to access the data and return data to a view by setting Viewdata["MyData"] from the controller.
Now my question is how to set individual data of a user control which has multiple instances in a MVC page.
Eg. I have a text box inside userctl.ascx as:
<%= Html.TextBox("ApprovalDate", ViewData["ApprovalDate"],
new { #ReadOnly = "True" })%>
If i set ViewData["ApprovalDate"] to be something, how does it apply to individual control data?
Appreciate your help.
In your case it would work if you set your user controls (partial views in MVC terminology) to be strongly typed.
Then you can initialize the model and pass it to each individual partial view without those values getting mixed up.
Of course, you need to pass to the view the data for all hosted controls in some form.

Resources