MVC 3表单提交和持续的模型数据 [英] MVC 3 form post and persisting model data

查看:148
本文介绍了MVC 3表单提交和持续的模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我缺少MVC形式是如何工作的一些基础知识。我有一个具有五六个不同领域的用户可以搜索我的主页搜索表单。所以我有这个张贴到我的结果采取行动就好了。结果动作看起来是这样的:

I think I'm missing some fundamentals on how MVC forms work. I have a search form on my home page that has five or six different fields a user can search on. So I have this POSTing to my results action just fine. The Result action looks like this:

[HttpPost]
public ActionResult Results(SearchModel model)
{
    ResultsModel results = new ResultsModel();
    results.ResultList = SearchManager.Search(model).ToList();

    return View("Results", results);
}

我已经简化了这个帖子上面的方法,但这个想法是一样的。因此,这一切工作正常。我的结果页面显示与结果列表和我的用户是在以下网址:

I've simplified the above method for this post, but the idea is the same. So this all works fine. My results page shows up with the list of results and my user is at the following URL:

http://www.site.com/results

所以......现在我想做一些事情相当普遍。我有结果页上的两个下拉列表。 排序和每页结果#。我如何做到这一点,送全套模型数据的回控制器这样我就可以用新参数查询?在现实中,SearchModel类有近60个不同的领域。潜在的所有数据可以被包含在模型中。你怎么坚持的一个网页后回到?

So...now I want to do something fairly common. I have two dropdown lists on the results page. "Sort by" and "# of results per page". How do I do that and send the full set of model data back to the controller so I can query with the new parameters? In reality, the SearchModel class has about 60 different fields. Potentially all of that data could be contained in the model. How do you persist that to a page "post back"?

这同一个问题有我如何做分页以及有点难倒。我的分页链接会去一个网址,如:

This same question has me a little stumped about how to do paging as well. My paging links would go to a URL like:

http://www.site.com/results/2

但是,假设我们正在响应一个GET请求(我不希望在查询字符串数据的60场),并且是GET请求,我知道之间传递的模型数据不是这样的。

But that assumes that we're responding to a GET request (I don't want 60 fields of data in the querystring) and that the model data is passed between GET requests, which I know isn't the case.

正如我所说的,我觉得我缺少有关使用MVC 3,模式和形式岗位工作的一些基本原则。

As I said, I think I'm missing some fundamentals about working with MVC 3, models and form posts.

谁能帮点我在这里正确的方向?我会很乐意编辑/更新这个帖子是需要澄清的事情。

Can anyone help point me in the right direction here? I'll be happy to edit/update this post as needed to clarify things.

修改:我也想指出,我想避免存储在会话变量视图模型。这个网站将最终结束了作为一个Web场负载平衡,我真的想尽量避免使用会话。但是,如果这是唯一的选择,我会配置另一个会话状态提供者,但我preFER没有。

EDIT: I also wanted to point out, I'd like to avoid storing the view model in a Session variable. This site will eventually end up being load balanced in a web farm and I'm really trying to avoid using Session if possible. However, if it's the only alternative, I'll configure another session state provider, but I'd prefer not to.

推荐答案

您可以添加您当前SearchModel参数的路线值表单。 BeginForm几个版本允许你在一个对象/ RouteValues​​Dictionary通过。

You can add your current SearchModel parameters to the route values for your form. Several versions of BeginForm allow you to pass in an object/RouteValuesDictionary.

@Html.BeginForm("Action", "Controller", new { SearchModel = Model }, FormMethod.Post)

这应该直通当前的SearchModel值,以便可以重新利用它们来获取下一个页面。您需要定义一个控制器动作将接受任何当前页面的表单值还有SearchModel。

This should pass-through your current SearchModel values so you can re-use them to get the next page. You need to have a controller action defined that will accept any current-page form values as well as the SearchModel.

我还没有与形式的职位做到了这一点,但我做了什么,并从文档说什么,这是我将开始。当然,这也意味着每个页面上的页码链接将需要做的职位。如果他们希望能够在浏览器中使用后退按钮对于用户来说确实不方便。

I have not done this with form posts, but from what I have done and from what the docs say, this is where I would start. Of course, this also means that each of your page number "links" on the page will need to be doing posts. That is really inconvenient for users if they want to be able to use the Back button in the browser.

在这种情况下,你可以尝试定义,允许页码显示为URL的一部分的路线 - 动作/控制器/ {PAGE}。但是,我不知道怎么会因为形式正在做后期工作。

In this context, you can try to define a route that allows the page number to appear as a part of the URL -- "Action/Controller/{page}". However, I am not sure how that will work given that the form is doing a post.

回应评论:

是的,你可以使用路由值的SearchModel添加到每个网页链接,但正如我在评论说,如上所述,由于链接将做一个让,用户将看到SearchModel序列化为的一部分链接。

Yeah, you can use Route Values to add the SearchModel to each page link, but as I said in the comment above, since the links will do a "get," your users will see the SearchModel serialized as a part of the link.

无论哪种方式,使用路由值是你的答案又回到原来的SearchModel不使用隐藏域,会话或TempData的。

Either way, using Route Values is your answer to getting back your original SearchModel without using hidden fields, Session, or TempData.

这篇关于MVC 3表单提交和持续的模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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