Asp.net MVC-如何使用EntityFramework添加order-orderDetails对象?

发布于 2024-08-12 16:16:17 字数 239 浏览 5 评论 0 原文

我有一个应用程序,它使用一对多关系对象,例如 oreder-orderDetails、EntityFramework。

我想使用一个视图,它可以添加带有一些 orderDetails 对象的新订单。

我可以创建一个强类型视图,它返回一个具有 List orderDetails 属性的订单对象,但无法填充 orderDetails。

有人有解决办法吗,该怎么做?

预先感谢

加布里埃尔

I have an application that uses one-to-many relationshipped objects like oreder-orderDetails, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃扇骨 2024-08-19 16:16:17

我认为您需要使用订单表而不是订单视图来执行此操作。

I think that you need to use the order table instead of the order view to do this.

清晰传感 2024-08-19 16:16:17

好的,假设您创建一个名为 OpenOrders 的视图,该视图继承自 Order 实体。

<%@ Page Language="C#"  
Inherits="System.Web.Mvc.ViewPage<List<OrdersApp.Models.Order>>" %>

因此,您可以显示订单列表中的项目,还可以添加一个表单,允许
输入订单详细信息。

所以当你发布到页面时你可以收到Orders对象和FormCollection对象

//
// POST: /Orders/OpenOrders/Details

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenOrders(Order order, FormCollection collection)

然后你可以创建一个新的OrderDetails对象并将其添加到Orders对象中然后保存。

OrderDetails orderdetails = new OrderDetails()

orderdetails.Description = = collection["OrderDescription"].ToString();
...
...

order.OrderDetails.Add(orderdetails);
orderRepository.Save();

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();
捶死心动 2024-08-19 16:16:17
  • 主题添加订单orderDetails

    1. 添加到订单表

    2. 通过orderID选择订单表中的列表订单

    3. 检查订单详细信息中的产品是否有重叠

    4. 循环认为listorderdetail中的每个orderdetail,添加到数据库中

    5. 检查某些条件,例如:产品表中的产品数量...

-可以吗?

  • Topic add order orderDetails

    1. Add into Order table

    2. Choose list order in a order table by orderID

    3. Check products in orderdetail for the overlap

    4. loop thought every orderdetail in listorderdetail, add into database

    5. Check some condition, for example: quantity of product in product table ....

-Ok?

空城之時有危險 2024-08-19 16:16:17

我在视图中使用了以下代码来填充列表中的数据。

    @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>
        }
     }

您可以在这里使用其他控件,例如 listview 或 gridview 等。

视图模型如下:

    public class RefvaluesDetailViewModels
      {

    public String RefID { get; set; }
    public List<RefLocaleCreateEditModels> ReferenceTranslateion { get; set; }
       }

也许这可以帮助您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文