ASP.NET MVC2 ORDER/ORDER LINE (Master/Detail) View - how to tie everything up
I've created this form: (code below)
Order Header
Order Item List
Order Payment List
Order Summary
I'm populating the list using Steve Sanderson method (jquery)
When I post the Form I get only the Header fields. How can I tie the 2 Lists Fields into the model (the Order inside the view model contain the enumeration for those list, and it is used to populate them. I need a way to send them back Plus the added rows of both list.
here is the page code:
<h2>
<%: Html.Label(Model.order.OrderID.ToString()) %></h2>
<% using (Html.BeginForm("EditOrder", "Orders", FormMethod.Post, Model))
{%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>OrderDetails</legend>
<div>
<%: Html.Label("Order Date: ")%>
<%: Html.TextBoxFor(o => o.order.OrderDate)%>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<fieldset>
<legend>Order Items:</legend>
<div id="editorRows">
<% foreach (var item in Model.order.ItemsInOrders)
{
Html.RenderPartial("ItemEditorRow", item);
Html.Display("<br/>");
}
%>
</div>
<%= Html.ActionLink("Add...", "BlankItemRow", null, new { id = "addItem" }) %>
</fieldset>
<fieldset>
<legend>Order Activities:</legend>
<div id="ActivityRows">
<% foreach (var item in Model.order.ActivityInOrders)
{
Html.RenderPartial("ActivityEditorRow", new TechRun.UI.Models.OrderActivityViewModel { acti开发者_如何学编程vity = item, activitiesList = Model.activities });
Html.Display("<br/>");
}
%>
</div>
<%= Html.ActionLink("add...", "BlankActivityRow", null, new { id = "addItem2" })%>
</fieldset>
<fieldset>
<legend>Order Summary:</legend>
</fieldset>
<%} %>
EDIT: UPDATED:
Here is the Partial Views:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TechRun.Shared.ItemsInOrder>" %>
<%@ Import Namespace="TechRun.UI.HtmlHelpers"%>
<div class="editorRow">
<% using (Html.BeginCollectionItem("items"))
{ %>
ID: <%: Html.TextBoxFor(x => x.ItemID)%>
ITEM: <%: Html.TextBoxFor(x => x.ItemDescription) %>
PRICE: <%: Html.TextBoxFor(x => x.CostOfItem, new { size = 4 }) %>
NUME: <%: Html.TextBoxFor(x => x.NumOfItems, new { size = 4 }) %>
DatE: <%: Html.TextBoxFor(x => x.SupplyDate) %>
Total: <%: Html.Label((Model.CostOfItem * Model.NumOfItems).ToString())%>
<!-- This might be wrong -->
<%: Html.HiddenFor(x=>x.TotalCost,Model.CostOfItem * Model.NumOfItems) %>
<%: Html.HiddenFor(x=>x.OrderID, Model.OrderID) %>
<% } %>
</div>
<br />
And the other Row Generator:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TechRun.UI.Models.OrderActivityViewModel>" %>
<div class="ActivityRows">
<% using (Html.BeginCollectionItem("activities"))
{ %>
<% IEnumerable<SelectListItem> myActivityList = null;
if(Model.activitiesList != null)
{
myActivityList = Model.activitiesList.Select(o => new SelectListItem
{
Text = o.ActivityID +
" - " + o.ActivityName + " Price: " + o.Cost + " TIME: " + o.ActivityTimeHours,
Value = o.ActivityID.ToString(),
Selected = (o.ActivityID == Model.activity.ActivityID)
});
}
else return ; %>
ID:
<%: Html.DropDownListFor(x => x.activity.ActivityID, myActivityList)%>
PRICE:
<%: Html.TextBoxFor(x => x.activity.Cost, new { size = 4 })%>
TIME:
<%: Html.TextBoxFor(x => x.activity.TimeHours, new { size = 4 })%>
NUM:
<%: Html.TextBoxFor(x => x.activity.Items)%>
TOTAL:
<%: Html.Label((Model.activity.Items * Model.activity.Cost).ToString())%>
<%: Html.HiddenFor(x => x.activity.Total, Model.activity.Cost * Model.activity.Items)%>
<%: Html.HiddenFor(x => x.activity.OrderID, Model.activity.OrderID)%>
<% } %>
</div>
<br />
And... the prefix Generating coding - copied from here
Your Html.BeginCollectionItem("items") lines need to be something like Html.BeginCollectionItem("order.ItemsInOrders")
where the parameter is the name of the property containing the items.
To be honest, I haven't tried this with a nested object. You may need to surface the list on the initial model first. Something like:
public xxxx order_ItemsInOrders
{
get { return order.ItemsInOrders; }
set { xxxx } // may not bee needed
}
and then
<div id="editorRows">
<% foreach (var item in Model.order_ItemsInOrders)
{
Html.RenderPartial("ItemEditorRow", item);
Html.Display("<br/>");
}
%>
</div>
also ItemsInOrders or ItemsInOrders needs to be delared as a property.
精彩评论