ASP.Net MVC 3 RC2,部分视图表单处理

发布于 2024-10-08 05:26:29 字数 503 浏览 0 评论 0原文

经过多年的 Web 窗体开发,我决定尝试一下 MVC。我已经能够克服大多数其他障碍,但我有点卡在这个障碍上。

在 Web 表单中,我将为每个表单创建控件以进行模块化,以便我可以在整个站点中重复使用它们。我试图在 MVC 中做同样的事情,但有部分观点,但运气不佳。这可能很简单,也许我只是没有搜索正确的关键字集来找到明显的例子。

假设我为登录表单创建了一个强类型部分视图。我想将其包含在我网站的几个页面上。由于代码与视图是分离的,如何封装逻辑以便可以在多个视图中轻松重用?在 Web 表单中,我只是将其放在代码隐藏中。在 MVC 中,我是否必须将代码放入每个要使用该控件的视图控制器中?

其次,您如何处理来自部分视图的帖子?我尝试创建强类型视图和部分视图,但视图模型似乎不会像表单直接位于视图上时那样自动填充。我始终可以使用 FormCollection 作为操作参数,并将值强制到我的部分视图模型中,但这看起来不太优雅。有人可以向我指出一些功能示例,说明如何在单个视图上使用带有表单的多个强类型部分页面(例如,带有登录控件和注册控件的登录页面)

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.

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.

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?

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失与倦" 2024-10-15 05:26:29

在 MVC 中,您不应在 View 中保留任何逻辑(除了仅查看位)。
例如,我建议查看 Fabrikam Shipping

为了保持视图干燥,您可以使用部分和编辑/视图模板。
要提交部分表单 - 您可以使用 jQuery AJAX(Hijax):

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

此外,如果您想将整个表单提交给其他操作或控制器,您可以使用如下内容:

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

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

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"}))
挽容 2024-10-15 05:26:29

将其放在 Views\Helpers 文件夹中:

@helper LoginControl() {
    @using (Html.BeginForm("Login","Home")) {

    }
}

并在您的视图或主控中使用它:

<div>
    @LoginControl()
</div>

现在只需将其添加到您的 HomeController

public ActionResult Login(LoginModel model)
{
}

Place this in Views\Helpers folder:

@helper LoginControl() {
    @using (Html.BeginForm("Login","Home")) {

    }
}

And use this in your views or the master:

<div>
    @LoginControl()
</div>

now simply add this to your HomeController

public ActionResult Login(LoginModel model)
{
}
梅倚清风 2024-10-15 05:26:29

对于 MVC3 中的 Razor 助手,我们没有真正干净的解决方案。在常规网站中,您可以将 CSHTML 或 VBHTML 文件放入 App_Code,然后使用 jgauffin 建议的语法。不幸的是,我们无法实现他提到的 ~/Views/Helpers 模型,因为我们的编译系统存在一些限制,我们无法克服 v1 的限制(我们正在研究 v2)。

David Ebbo 是 ASP.Net 团队的架构师,他提出了一个使用 Visual Studio 代码生成器的解决方案,这样您就可以将助手编译成任何 dll(甚至是常规类库): http://blogs.msdn.com /b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx。我强烈建议检查一下

We don't have a really clean solution for Razor helpers in MVC3. In a regular web site, you can put a CSHTML or VBHTML file in App_Code and then use the syntax jgauffin suggested. Unfortunately, we weren't able to implement the ~/Views/Helpers model he mentioned due to some limitations in our compilation system we weren't able to overcome for v1 (we're investigating for v2).

David Ebbo, an architect on the ASP.Net team, came up with a solution that uses Visual Studio Code Generators so you can compile your helpers into ANY dll at all (even a regular class library): http://blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx. I highly recommend checking that out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文