MVC3:如何发布包含部分视图的表单?

发布于 2024-11-01 04:56:12 字数 329 浏览 1 评论 0原文

工具:MVC3、jQuery Unobtrusive Validation、Razor、VS 2010

我正在开发一个 MVC2 项目,使用户能够请求服务。我已将所有表单通用的信息放置在部分视图中,这些视图对它们自己的模型进行强类型化。每个部分视图都有自己的控制器。部分视图在主容器页面中呈现。我对渲染页面上的所有数据都有不显眼的 jQuery 数据验证。

问题: 编写将所有页面数据中继到服务器的帖子的最佳方法是什么?如何将部分视图关联到各自的模型?部分视图的控制器是否可以处理自己的数据存储杂务?有什么好的例子吗?或者,这个架构是否有缺陷,我应该重新考虑?

预先感谢,

阿诺德

Tools: MVC3, jQuery Unobtrusive Validation, Razor, VS 2010

I am developing an MVC2 project that enables users to request services. I have placed information common to all forms in partial views, which are strongly typed to their own models. Each partial view has its own controller. The partial views are rendered in the main container page. I have unobtrusive jQuery data validation working for all data on the rendered page.

Questions: What is the best way to code a Post that relays all the page data to the server and how can I associate the partial views to their respective models? Is it possible for the controllers for the partial views to handle their own data storage chores? Any good examples somewhere? Or, is this architecture flawed and I should rethink?

Thanks in advance,

Arnold

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

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

发布评论

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

评论(1

成熟稳重的好男人 2024-11-08 04:56:13

不,一点也不,听起来很好分解并且易于测试。首先,确保表单已在 HTML 中设置正确的操作、方法等。因此,要发布整个页面,您可以执行以下操作:

var savePage = function () {
    $('form').each(function (formIndex, formElement) {
        var f = $(formElement);
        $.post(f.attr('action'), f.serialize(), successfulFormPost);
    });
};

var successfulFormPost = function (data) { ... };

现在,如果您的 MVC 视图如下所示:(

请注意 name 属性的命名约定)。然后,您可以使该表单的控制器接受与视图的 @Model 匹配的强类型参数:

public class SomeModel {
    public int Id { get; set; }
    public string Description { get; set; }
}

public class SomeController : Controller {
    [HttpPost]
    public ActionResult SomeAction(SomeModel someModel) {
        // use someModel.Id, someModel.Description here
    }
}

我更手动地完成了该 HTML,但我只是证明有关将 HTML POST 与控制器操作绑定和链接的一点。我将让您使用 Html.TextBox 类型语法引入不显眼的验证。只需记住根据默认活页夹的工作方式设置输入字段的名称属性:

http://www.asp. net/mvc

这是所有这些基础知识的重要来源。

No, not at all, sounds nicely broken up and easy to test. First off, make sure the forms are well set up with the right action, method, etc. in HTML. So then to post the whole page you could do something like this:

var savePage = function () {
    $('form').each(function (formIndex, formElement) {
        var f = $(formElement);
        $.post(f.attr('action'), f.serialize(), successfulFormPost);
    });
};

var successfulFormPost = function (data) { ... };

Now, if your MVC view looks something like this:

(Notice the naming convention for the name attribute). Then you can make your controller for that form take in a strongly typed parameter that matches the view's @Model:

public class SomeModel {
    public int Id { get; set; }
    public string Description { get; set; }
}

public class SomeController : Controller {
    [HttpPost]
    public ActionResult SomeAction(SomeModel someModel) {
        // use someModel.Id, someModel.Description here
    }
}

I did that HTML a little more manually, but I'm just proving a point about binding and linking up HTML POST with controller actions. I'll leave it up to you to bring in unobtrusive validation by using the Html.TextBox type syntax. Just remember to set the name attribute of your input fields according to how the default binder works:

http://www.asp.net/mvc

That's a great source for all these fundamentals.

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