Asp.net MVC-How to add order-orderDetails objects using EntityFramework?
I have an application that uses one-to-many relationshipped objects like oreder-o开发者_StackOverflow中文版rderDetails, vith EntityFramework.
I want to use a view, that make it possible to add a new order with some orderDetails objects.
I can create a strongly typed view, that returns an order object wich has a List orderDetails property, but can't populate the orderDetails.
Has anybody a solution, how to do that?
Thanks in advance
Gabriel
I think that you need to use the order table instead of the order view to do this.
Ok, say you create a view named OpenOrders that inherits from your Order entity.
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<List<OrdersApp.Models.Order>>" %>
So then you can display items from your orders list and also add a form that will allow the input of Order Details.
So when you post to the page you can receive the Orders object and FormCollection object
//
// POST: /Orders/OpenOrders/Details
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenOrders(Order order, FormCollection collection)
Then you can create a new OrderDetails object and add it to the Orders object and then save.
OrderDetails orderdetails = new OrderDetails()
orderdetails.Description = = collection["OrderDescription"].ToString();
...
...
order.OrderDetails.Add(orderdetails);
orderRepository.Save();
Topic add order orderDetails
Add into Order table
Choose list order in a order table by orderID
Check products in orderdetail for the overlap
loop thought every orderdetail in listorderdetail, add into database
Check some condition, for example: quantity of product in product table ....
-Ok?
I have used following code on my view to populate the data in my List.
@model Webrixs_Portal.Web.Models.CustomViewModels.RefvaluesCreateEditModels
@using (Html.BeginForm("EmbadedEditor", "Refvalues", FormMethod.Post, new { id = "SavingRefValueInTable" }))
{
@for (var i = 0; i < Model.ReferenceTranslateion.Count(); i++)
{
<div class="row" style="margin-left:0px;margin-right:0px;padding-left: 0px !important; padding-right: 0px !important;margin-top:5px;">
<div class="col-md-3" style="padding-right:0px;padding-left:3px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;">@Model.ReferenceTranslateion[i].RelLanguageName</div>
<div class="col-md-9" style="padding-right:0px;padding-left:0px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;">
@Html.TextBoxFor(model => model.ReferenceTranslateion[i].RelTe1, new { @class = "k-textbox full" })
</div>
@Html.HiddenFor(model => model.ReferenceTranslateion[i].RelID)
@Html.HiddenFor(model => model.ReferenceTranslateion[i].RelLangID)
</div>
}
}
You can use other controls here like listview or gridview etc.
The view model is like:
public class RefvaluesDetailViewModels
{
public String RefID { get; set; }
public List<RefLocaleCreateEditModels> ReferenceTranslateion { get; set; }
}
Maybe this can help you.
精彩评论