我正在编写一个页面,允许用户将值输入到文本框中,然后将其添加到集合中。
有2个循环:
需要发生的是当他们单击UpdateAction按钮时,表单将DelegatesList [x]值(x是唯一OrderLine的委托列表)传递给控制器。然后,控制器需要从视图(x列表)更新相关Orderline的OrderLines.DelegatesList集合以及DelegatesList的所有行。
我理解逻辑,但我很难理解如何将值传递到控制器中,然后在控制器的循环中更新集合。非常感谢任何帮助
查看:
@model TSW.Web.ViewModels.Partial.BasketSummary
@using (@Html.BeginUmbracoForm<BasketPageSurfaceController>
("Update"))
<article id="basket-item-delegate__main" class="basket-item-delegate__main" data-id="@i">
<div class="basket__items">
<div class="basket-item-delegate">
@for (int x = 0; x < @Model.CustomerBasket.OrderLines[i].Quantity; x++)
{
var y = x + 1;
<div class="basket-item-delegate-list">
<h4>Delegate @y</h4>
<div class="basket-item-delegate__input-cell">
<h6>First Name:</h6>
@Html.TextBoxFor(m => @Model.CustomerBasket.OrderLines[i].DelegatesList[x].FirstName)
</div>
<div class="basket-item-delegate__input-cell">
<h6>Last Name:</h6>
@Html.TextBoxFor(m => @Model.CustomerBasket.OrderLines[i].DelegatesList[x].LastName)
</div>
<div class="basket-item-delegate__input-cell">
<h6>Email</h6>
@Html.TextBoxFor(m => @Model.CustomerBasket.OrderLines[i].DelegatesList[x].Email)
</div>
</div>
}
<nav class="basket-item-delegate__buttons">
<input type="submit" name="UpdateAction" value="Add Delegates" class="basket-item-delegate__button button" />
</nav>
</div>
</div>
</article>
购物篮摘要VM:
public class BasketSummary
{
public Basket CustomerBasket { get; set; }
public List<Event> BasketEvents { get; set; }
public LinkItem CheckoutPage { get; set; }
public LinkItem DelegatePage { get; set; }
public JObject ECommerceSummary { get; set; }
public Event OrderEvent(int eventId)
{
return BasketEvents.FirstOrDefault(x => x.Id == eventId);
}
}
OrderLine VM:
public class OrderLine
{
[Required(ErrorMessage = "Quantity must be a number")]
public int Quantity { get; set; }
public List<OrderDelegate> DelegatesList { get; set; }
}
OrderDelegate VM:
public class OrderDelegate
{
[Key]
public int OrderLineId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
编辑:控制器
public async Task<ActionResult> Basket()
{
var model = Mapper.Map<BasketVM>(CurrentContent);
model.BasketSummary.CustomerBasket = await TransactionLibrary.GetBasketAsync();
model.BasketSummary.CustomerBasket.AllowBasketUpdates = true;
model.BasketSummary.BasketEvents = new List<Event>();
model.InnerPageHeader.ShowHeading = true;
model.InnerPageHeader.Title = model.PageTitle;
await model.BasketSummary.CustomerBasket.OrderLines.ForEachAsync(async dt =>
{
var eventDetail = await _eventService.GetEvent(dt.EventId);
model.BasketSummary.BasketEvents.Add(eventDetail);
var delegatelist = new List<OrderDelegate>
});
model.BasketSummary.CheckoutPage = Mapper.Map<LinkItem>(_pageFactory.GetCurrentHomepage().CheckoutPage);
if (Session["ErrorList"] != null)
{
var errorList = Session["ErrorList"] as IEnumerable<KeyValuePair<string, ModelState>>;
foreach (var modelState in errorList)
{
ModelState.Add(modelState);
}
Session.Remove("ErrorList");
}
return View(model);
}