局部视图表单处理

局部视图表单处理

本文介绍了ASP.Net MVC 3 RC2,局部视图表单处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多年的Web窗体的发展,我决定给MVC一展身手。我已经能够通过其他大部分绊脚石的工作,但我有点坚持这一点。

After years of Web Forms development I've decided to give MVC a go. I've been able to work through most of the other stumbling blocks, but I'm kind of stuck on this one.

在Web窗体我会以模块化所以在我的网站,我可以重新使用它们创建我的每个窗体控件。我试图做同样的事情在MVC但有部分意见和我没有多少运气。这也可能是简单的东西,也许我只是没有一套正确的关键字搜索找到明显的例子。

In Web Forms I would create controls for each of my forms in order to modularize so I could re-use them throughout my sites. I am attempting to do the same thing in MVC but with partial views and am not having much luck. It's probably something simple, and maybe I'm just not searching on the right set of keywords to find the obvious example.

可以说,我创建一个强类型的登录表单局部视图。我想包括它在我的网站的几个页面。由于code是分开的观点,我怎么封装逻辑,所以它可以在几个视图便于重用?在Web窗体,我只是坚持在code-后面。在MVC我必须把code在我想用控制每个视图控制器?

Lets say I create a strongly typed partial view for a Login form. I want to include it on several pages of my site. Since the code is separate from the view, how do I encapsulate the logic so it can be easily reused in several views? In Web Forms, I just stuck it in the code-behind. In MVC do I have to put the code in every view controller where I want to use the control?

二,你如何处理从部分意见的帖子吗?我试着创建强类型的视图和局部视图,但视图模型似乎不填充时,形式是直接在视图自动地像他们这样做。我可以一直使用的FormCollection的操作参数和正义的力量值放入我的局部视图模型,但是这似乎并没有太高雅了。有人能指出我如何使用一些功能性的例子多强类型与形式的部分页面上的单一视图(例如一个登录控件和一个寄存器控制一个登录页面)

Second, how do you handle posts from partial views? I've tried creating strongly typed views and partial views, but the view models don't seem to populate automagically like they do when the form is directly on the view. I could always use the FormCollection as the action parameter and just force the values into my partial view models, but that doesn't seem too elegant. Can someone point me to some functional examples of how to use multiple strongly typed partial pages with forms on a single view (e.g. A login page with a login control and a register control)

推荐答案

在MVC你不应该密切留意任何逻辑(除了仅查看位)。结果
对于例子,我建议看。

In MVC you should not keep any logic in View(except View only bits).
For examples I suggest to look at Fabrikam Shipping.

要保持你的浏览干燥,可以使用局部模板和编辑/查看模板。
提交表单的一部分 - 你可以使用jQuery AJAX(Hijax):

To keep your Views DRY, you can use Partials and Edit/View templates.To submit part of form - you can use jQuery AJAX(Hijax):

$("#someForm").submit(function () {
            $.post($(this).attr("action"), $(this).serialize(),
                  function (response) {
                       $("#someContainer").append(response);
                  });
            return false;
        });

此外,如果你想提交整个表单到其他行动或控制器,你可以使用这样的:

Also if you want to submit whole form to other action or controller, you can use something like this:

@using (Html.BeginForm("Action","Controller",FormMethod.Post,new{id="Form"}))

这篇关于ASP.Net MVC 3 RC2,局部视图表单处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:34