asp.net c# MVC: How do I live without ViewState?

2019-01-21 00:08发布

I am just looking into converting WebForms to MVC:

In .net MVC, what concepts make ViewState something thats not required?

If a form is posted back on iteself etc (ie a postback)? how does the page/usercontrol maintain its state?

What tricks are people doing to maintain some kind of state and not resort to session state?

Surely, a completely stateless environment cant exist?

13条回答
疯言疯语
2楼-- · 2019-01-21 00:21

After reading all these posts, it sounds like MVC is not good for LOB kind of applications, where you will have lot of controls and, CRUD operations and you want to maintain the state of the controls. There are plenty of reasons where you want the user to stay on the same view & maintain state after submit operations is done. For example to show errors, server side validation messages, success messages, or to perform any other action. redirecting user to some other view to show these messages is not practical.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-21 00:23

The state is the model which is in the database. You can carefully cache the database to reduce page load times.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-21 00:25

viewstate is just a big, ugly hidden form field.

Write out your own hidden form fields, and encrypt them if you have to.

Fortunately, there's no longer any simple way to dump lots and lots of data into the page, so you have to be judicious about what you want to save.

查看更多
Viruses.
5楼-- · 2019-01-21 00:26

You can store any object in the session state.

 HttpContext.Session["userType"] = CurrentUser.GetUserType();
查看更多
虎瘦雄心在
6楼-- · 2019-01-21 00:27

Actually it does. You have to forget about the way persistance was made up with the viewstate.

You have also to convert in your mind postback onto a page to "call to a controller". This way things will be easier to understand afterwards. Instead of calling a page , you're calling a controller which returns a view. So either your are building your whole "page" again and again on every call, or you decide to deal only with what is really impacted by the action. If the button is changing a div, why reload the entire page. Just make you call to your controller and return what should be the new data in your div.

for example let's imagine a master/detail scenario:

<h2>Groups</h2>
    <div id="GroupList">
    </div>
    <div id="GroupDetail" title="Detail Group">
</div>

The list of group is loaded once in the div and there is an ajax call possible to a controller for each item of the list of group :

<%= Ajax.ActionLink("Edit", "DetailLocalisationGroup", 
                     new { id = group.Id }, 
                     new AjaxOptions() { 
                         UpdateTargetId = "DetailLocalisationGroup", 
                         OnSuccess = "InitialisationDetailGroup" })%>

which calls this action DetailLocalisationGroup which is to feed the div GroupDetail with html.

[AcceptVerbs("POST")]
public ActionResult DetailLocalisationGroup(int id)
{
    LocalisationGroup group = servicelocalisation.GetLocalisationGroup(id);
    return View("DetailGroup", group);
}

There is now a form in the div, and when pushing the submit button of this form, we just send the information we really need to a controller which would then save the data in the database.

During all these events, the GroupList was filled with stuff that was displayed on the client screen, but no interaction was needed there and so why bother oneself with a viewstate for these...

查看更多
我只想做你的唯一
7楼-- · 2019-01-21 00:28

Auto generated view state does not exist in MVC, but you can write your own simply using hidden fields,

In MVC you will not see a lot of encrypted chars at the top of the page which you don't need most of them.

查看更多
登录 后发表回答