如何在jquery中使用模型绑定 [英] How to use model binding with jquery
问题描述
MVC模型绑定程序在从页面中获取值并将其作为属性输入到我的操作方法中的工作非常出色.
The MVC model binder is doing a great job of picking up values from my page and passing them into my action methods as properties in my complex input.
我正在尝试创建购物车类型的页面,用户可以在其中配置订单项列表,然后提交整个订单.
I'm trying to create a shopping cart type of page, where the user can configure a list of line items, then submit the entire order.
起初,我在用户要添加项目时使用Ajax表单-现有行项目的列表将传递回控制器,添加新行项目,并返回该列表的局部视图.
At first I was using Ajax forms for when the user wanted to add an item -- the list of existing lineitems would be passed back to the controller, a new lineitem would be added, and a partial view for the list returned.
不幸的是,提交Ajax表单后,您似乎无法重定向到另一个页面.我不想在jQuery中这样做,因为当MVC模型绑定程序做得如此好时,我不想处理显式创建复杂对象的列表.
Unfortunately, looks like you can't redirect to another page after an Ajax form is submitted. I don't want to do it in jQuery though, because I don't want to deal with explicitly creating the list of complex objects when the MVC model binder does such a good job of it.
澄清一下,这是我的问题:
To clarify, here is my problem:
我希望我的表单有2个操作,这两个操作都需要将行项列表传递回去.
I want my form to have 2 operations, both of these require the list of lineitems to be passed back.
据我了解,我必须至少使用javascript进行这些操作之一.要么
It is my understanding that I have to do at least one of these operations using javascript. Either:
- 使用HtmlForm提交整个订单,返回新视图
- 单击添加项目"按钮时,使用jQuery添加lineitem
OR
- 使用AjaxForm动态添加新的订单项
- 提交订单后,使用javascript进行重定向,但这意味着我也必须使用javascript提交数据
有没有一种方法可以在不使用JavaScript的情况下做到这一点?当movdel活页夹没有任何工作时,我不想使用JSON自己创建lineitems列表.
Is there a way I can do this without using javascript? I don't want to create the list of lineitems myself with JSON when the movdel binder does it without any work on my part.
推荐答案
好,我想了解您的需求...
Ok, I think understand what you need...
- 从表单操作属性获取目标操作网址
- 序列化表格以将其发送给控制器
并重定向到新页面.
$.ajax({
url: $('#FormID').attr('action'),
type: 'POST',
data: $('#FormID').serialize(),
success: function (result) {
window.location = result;
}
});
此处是一些文档信息 API jquery .serialize()
这篇关于如何在jquery中使用模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!