ASP.NET MVC和的ViewState [英] ASP.NET MVC and ViewState

查看:314
本文介绍了ASP.NET MVC和的ViewState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经看到了这样的一些问题,但它不正是我想要问什么,所以对于那些尖叫重复的,我道歉:)

Now I've seen some questions like this, but it's not exactly what I want to ask, so for all those screaming duplicate, I apologize :).

我几乎没有触及ASP.NET MVC,但是从我的理解是没有的ViewState /了ControlState- ...罚款。所以我的问题是,什么是替代保留了控件的状态呢?难道我们回到老同学ASP,我们可以模拟一下ASP.NET的ViewState /了ControlState-确实通过与控制的状态创建隐藏的表单输入,或MVC,我们只是假设AJAX始终,并保留所有州的客户端,使AJAX调用更新?

I've barely touched ASP.NET MVC but from what I understand there is no ViewState/ControlState... fine. So my question is what is the alternative to retaining a control's state? Do we go back to old school ASP where we might simulate what ASP.NET ViewState/ControlState does by creating hidden form inputs with the control's state, or with MVC, do we just assume AJAX always and retain all state client-side and make AJAX calls to update?

这问题有一定的答案,<一个href="http://stackoverflow.com/questions/1285547/maintaining-viewstate-in-asp-net-mvc">http://stackoverflow.com/questions/1285547/maintaining-viewstate-in-asp-net-mvc,但我的答案找不完全是。

This question has some answers, http://stackoverflow.com/questions/1285547/maintaining-viewstate-in-asp-net-mvc, but not exactly what I'm looking for in an answer.

更新:感谢所有的答案为止。只是为了澄清什么,我不是在寻找什么,我在寻找:

UPDATE: Thanks for all the answers so far. Just to clear up what I'm not looking for and what I'm looking for:

不找:

  • 会议解决方案
  • 曲奇解决方案
  • 不打算模仿的WebForms的MVC

我是什么/是寻找:

  • 仅保留在回发的状态,如果数据不反弹控制的方法。想想WebForms的,只有结合初始页面加载一个网格,即只需要重新绑定数据时的情景。正如我所说,我并不想模仿的WebForms,只是想知道什么样的机制MVC提供。

推荐答案

该公约已经可以不通过太多的篮球跳跃。诀窍是基于关闭传递到视图模型要连接文本框的值。

The convention is already available without jumping through too many hoops. The trick is to wire up the TextBox values based off of the model you pass into the view.

[AcceptVerbs(HttpVerbs.Get)]   
public ActionResult CreatePost()
{
  return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreatePost(FormCollection formCollection)
{
  try
  {
    // do your logic here

    // maybe u want to stop and return the form
    return View(formCollection);
  }
  catch 
  {
    // this will pass the collection back to the ViewEngine
    return View(formCollection);
  }
}

接下来会发生什么是视图引擎取的FormCollection和集合中的ID名称相匹配的键/值,你有你的观点,使用HTML帮手。例如:

What happens next is the ViewEngine takes the formCollection and matches the keys within the collection with the ID names/values you have in your view, using the Html helpers. For example:

<div id="content">

  <% using (Html.BeginForm()) { %>

  Enter the Post Title: <%= Html.TextBox("Title", Model["Title"], 50) %><br />
  Enter the Post Body: <%= Html.TextArea("Body", Model["Body"]) %><br />

  <%= Html.SubmitButton() %>

  <% } %>

</div>

注意文本框和文本区域有标题和正文的ID?现在,请注意我是如何从视图的Model对象设置值?既然你在的FormCollection过去了(你应该设置为强类型用的FormCollection视图),您现在就可以访问它。或者,没有强烈的打字,你可以简单地使用计算机[标题](我认为)。

Notice the textbox and textarea has the IDs of Title and Body? Now, notice how I am setting the values from the View's Model object? Since you passed in a FormCollection (and you should set the view to be strongly typed with a FormCollection), you can now access it. Or, without strongly-typing, you can simply use ViewData["Title"] (I think).

的你神奇的ViewState。这个概念被称为约定优于配置。

POOF Your magical ViewState. This concept is called convention over configuration.

现在,上面的code是在其最简单,最原始的形式使用的FormCollection。事情变得替代的FormCollection有趣,当你开始使用的ViewModels。您可以开始添加您的模型/的ViewModels的自己的验证,并有控制器泡了自定义验证自动错误。这是另一天,虽然一个答案。

Now, the above code is in its simplest, rawest form using FormCollection. Things get interesting when you start using ViewModels, instead of the FormCollection. You can start to add your own validation of your Models/ViewModels and have the controller bubble up the custom validation errors automatically. That's an answer for another day though.

我会建议使用PostFormViewModel而不是Post对象,但每一个 - 他 - 自己。无论哪种方式,通过要求的对象上的操作方法,你现在得到一个isValid()方法可以调用。

I would suggest using a PostFormViewModel instead of the Post object, but to each-his-own. Either way, by requiring an object on the action method, you now get an IsValid() method you can call.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreatePost(Post post)
{

  // errors should already be in the collection here
  if (false == ModelState.IsValid())
    return View(post);

  try
  {
    // do your logic here

    // maybe u want to stop and return the form
    return View(post);
  }
  catch 
  {
    // this will pass the collection back to the ViewEngine
    return View(post);
  }
}

和你的强类型的视图将需要进行调整:

And your Strongly-Typed view would need to be tweaked:

<div id="content">

  <% using (Html.BeginForm()) { %>

  Enter the Post Title: <%= Html.TextBox("Title", Model.Title, 50) %><br />
  Enter the Post Body: <%= Html.TextArea("Body", Model.Body) %><br />

  <%= Html.SubmitButton() %>

  <% } %>

</div>

您可以把它更进一步,在视图中显示的错误,以及,直接从您的控制器设置的ModelState。

You can take it a step further and display the errors as well in the view, directly from the ModelState that you set in the controller.

<div id="content">

  <%= Html.ValidationSummary() %>

  <% using (Html.BeginForm()) { %>

  Enter the Post Title: 
    <%= Html.TextBox("Title", Model.Title, 50) %>
    <%= Html.ValidationMessage("Title") %><br />

  Enter the Post Body: 
    <%= Html.TextArea("Body", Model.Body) %>
    <%= Html.ValidationMessage("Body") %><br />

  <%= Html.SubmitButton() %>

  <% } %>

</div>

有趣的是这种方法是,你会发现我没有设置验证摘要,也没有在查看各个验证消息。我喜欢练DDD概念,这意味着被控制在我的领域,并获得通过在一个集合的形式,我的验证消息(和摘要)。然后,我循环throught他集合(如果存在任何错误),并将其添加到当前ModelState.AddErrors集合。剩下的就是自动当您返回查看(后)。

What is interesting with this approach is that you will notice I am not setting the validation summary, nor the individual validation messages in the View. I like to practice DDD concepts, which means my validation messages (and summaries) are controlled in my domain and get passed up in the form of a collection. Then, I loop throught he collection (if any errors exist) and add them to the current ModelState.AddErrors collection. The rest is automatic when you return View(post).

很多很多约定的超出。几本书我强烈建议覆盖这些模式在更多的细节是:

Lots of lots of convention is out. A few books I highly recommend that cover these patterns in much more detail are:

  • Professional ASP.NET MVC 1.0
  • Pro ASP.NET MVC 1.0 Framework

和按顺序的第一个涵盖了生坚果和整个MVC框架的具体细节。后者涵盖了微软官方RELM外先进技术,与一些外部的工具,使你的生活变得更轻松(温莎城堡,起订量,等等)。

And in that order the first covers the raw nuts and bolts of the entire MVC framework. The latter covers advanced techniques outside of the Microsoft official relm, with several external tools to make your life much easier (Castle Windsor, Moq, etc).

这篇关于ASP.NET MVC和的ViewState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆