在局部视图MVC3形式 [英] MVC3 form in partial view

查看:86
本文介绍了在局部视图MVC3形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找最好的方式来完成在MVC3以下,JQuery的AJAX:

I'm looking for the best way to accomplish the following in MVC3, JQuery AJAX:

我有购物车,其中一个可以进入不同的船,以解决在车的每个产品。 这个想法是有这样的船,形成正确的每一个产品下,还有一个保存按钮,并将它通过JQuery AJAX提交。

I have a shopping cart where one could enter a different ship to address for each product in the cart. The idea is to have this ship to form right under each product, along with a Save button and have it being submitted via JQuery AJAX.

就是我一直在试图做到这一点是有一个局部视图里面的表格(连同Html.BeginForm,并在那里提交SAVE按钮),然后有部分观点foreach循环里面所呈现在视图(通过一个视图模型项目到局部视图的形式)。

The way I've been trying to do it was to have the form inside a partial view (along with the Html.BeginForm and the submit SAVE button in there), and then have the partial view being rendered inside a foreach loop on the view (passing a viewmodel item to the partial view form).

所有的购物车中的第一款产品工作正常,但是当我试图进入并提交其他任何形式的地址在车中的第二个或第三个产品,我居然拿到ID从第一个和更新之一。

All works ok for the first product in the cart, but when I try to enter and submit any other form address for the second or third product in the cart, I actually get the ID from the first one and update that one.

读通过谷歌,我发现有关模型绑定到一个表单的文章,但对于那些情况下,我有foreach循环之前,一个总Html.BeginBeginForm且只有一个单一的提交按钮。我如何对每个产品在每个形式提交(保存)按钮,并有通过Ajax调用其提交?

Reading through Google I found articles about model binding to a form, but for those cases I have one overall Html.BeginBeginForm before the foreach loop and only one single submit button. How do I have a submit (SAVE) button for each form under each product and have it submitted via an AJAX call?

千预先感谢您!

更新:

也许给更好地了解我在做什么这里的例子:

Maybe to give a better picture of what I'm doing here is the example:

在Cart.cshtml,主视图,我有:

In the Cart.cshtml, the main view, I have:

@foreach (var item in Model.CartItems)
{
   @Html.Partial("ShipToAddress", new AddressViewModel(item.CartDetailsId))
}

然后在ShipToAddress.cshtml,局部视图,我有:

Then in the ShipToAddress.cshtml, the partial view, I have:

@using (Html.BeginForm("SaveShipToAddress", null, FormMethod.Post))
{ 
  @Html.HidenFor(model => model.ItemId)
  @Html.TextBoxFor(model => model.Name)
  @Html.TextBoxFor(model => model.Address)
  <input type="submit" value="Save" id="saveShipToAddress" />
}

然后,我有联系,这种形式,并通过AJAX提交它的jQuery脚本文件。

And then I have the jquery script file that ties to this form and submits it via AJAX.

我觉得我的问题是,形式是局部视图里面,所以当被产生它具有相同的ID在车中的所有物品。

I think my problem is that the form is inside the partial view, so when it gets generated it has the same IDs for all the items in the cart.

谢谢!

推荐答案

在情况下,任何人都需要做的我最终通过添加的itemId属性的形式解决这个类似的东西:

In case anyone else needs to do something similar I ended up solving this by adding an itemId attribute to the form:

@using (Html.BeginForm("SaveShipToAddress", null, FormMethod.Post, new { @class = "frmShipToAddress", itemId = Model.ItemId.ToString() }))

,然后jQuery的文件,我一把抓住了:

and then the jquery file I've grabbed that:

var shipAddress = {   
itemId: form.attr('itemId'),
name: form.find('input[name="Name"]').val(),
address: form.find('input[name="Address"]').val() }

var jsonData = JSON.stringify(shipAddress);

和通过jsonData以Ajax调用,然后在控制器中,由于使用视图模型对象接收并正确分析。

and passed the jsonData to the ajax call, which was then received in the controller and correctly parsed due to using the viewmodel object.

这篇关于在局部视图MVC3形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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