0
我使用MVC 4Ajax.BeginForm
更新,但它僅更新的第一個元素<div>
局部視圖MVC 4 foreach循環
查看:
@model List<CSP1225.Models.Item>
@{
ViewBag.Title = "RecentItems";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="~/CSS/jobs.css" rel="stylesheet" />
@{ var convert = ViewBag.convert;
}
<div>
@foreach (var item in Model)
{
<div class="job-item">
<div class="inner">
<div class="span8 job-span">
<!--start of job list container div-->
<div id="ja-joblist">
<ol id="ja-searchjoblist">
<li class="job-item">@{ var PriceLE = @item.Price * convert;}
@using (@Ajax.BeginForm("_AddToCart", "Home", item, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "cart" }, null))
{
<!-- job item right section-->
<div class="inner">
<div class="ja-job-meta clearfix">
<span class="ja-job-category"><a href="@item.ItemURL" target="_blank">@item.ItemName</a></span>
<span class="ja-job-category">@item.Price $</span>
<span class="ja-job-category">@PriceLE LE</span>
<div id="cart"></div>
<button type="submit">Add to Cart</button>
</div>
</div>
} </li>
<!-- end of job item right section-->
<!-- end of job item -->
</ol>
</div>
</div>
</div>
</div>
}
@Html.ActionLink("Go Back", "Index", "Home", new { @class = "makeneworder" })
</div>
和控制器:
public ActionResult _AddToCart(Item model)
{
ItemModel it = new ItemModel();
it.itemName = model.ItemName;
it.itemUrl = model.ItemURL;
it.quantity = 1;
it.unitprice = model.Price;
it.weight = (int)model.Weight;
it.ItemCategory =(int)model.CategoryID;
CartList.Add(it);
ViewBag.convert = (decimal)_db.Currencies.Where(x => x.Name == "Dollar").FirstOrDefault().Value;
ViewBag.list = CartList;
return PartialView();
}
局部視圖:
<p>Added to Cart</p>
但視圖返回多個元素(只要列表中包含的元素),當我點擊添加到購物車它更新的第一個元素..我明白,因爲你無法給另一<div>
相同的ID卻怎麼也我修復它?
你有'jquery.unobtrusive-ajax.js'納入這一頁? – Zabavsky