从一个模型添加数据到另一个模型? [英] Add data from one model to another?

查看:77
本文介绍了从一个模型添加数据到另一个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用模型显示数据库中的数据。在显示数据时,我添加了一个包含文本框的列,以便用户可以在该文本框中输入数量。提交数据后,我希望将数据存储在不同的表格中,如何实现。



例如



我有桌子库存

id

名称

mrp



查看我有四个栏目

id

name

mrp

(文本框)



视图数据应该提交到订单表。



我怎么能这样做。



我尝试过:



I am displaying data from database using a model. While displaying data I have added one more column with textboxes so that a user can input the quantity in that text box. After submitting the data I want that data to be stored in different table how can I achieve the.

for Example

I have a table Inventory
id
name
mrp

View I have four columns
id
name
mrp
(Textbox)

The View data should get submitted to Orders table.

How can I do this.

What I have tried:

@model IEnumerable<SaveTextboxes.Models.inventory>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.userLogin.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category.category1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Brand.Brand1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.productName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.mrp)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.prodDesc)
        </th>
        <th>Qty.
        </th>
        <th></th>
    </tr>
    @using (Html.BeginForm("Order", "Product"))
    {

        foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.userLogin.username)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Category.category1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Brand.Brand1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.productName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.mrp)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.prodDesc)
            </td>
            <td>
                I want to add this textbox and save it in orders table
                @Html.TextBoxFor(modelItem => item.qty)
            </td>
        </tr>
        }
        <tr>
            <td colspan="7">
                <button type="submit" value="Submit">Submit</button>
            </td>
        </tr>
    }

</table>

推荐答案

所以要确保我理解正确。



你有2个型号。库存和订单。这些模型也对应于库存和订单表格?



因为我在你的控制器中没有看到你的Action的任何代码,所以我要去做一个。



我会首先提供许多可能的解决方案之一,然后提供建议。



So to make sure I understand correctly.

You have 2 models. Inventory and Orders. These models correspond to tables of Inventory and Orders as well?

Since i don't see any code for your Action in your controller i'm going make one up.

I'll first provide one of many possible solutions, then provide a suggestion.

[HttpPost]
public ActionResult Order(Inventory model)
{
    //I think this is the syntax to get form values from a request. Either way, general idea, I didn't open VS cause I'm lazy
    var qty = Request.Form["qty"];
   //Here you would do the conversion from Inventory model to Order model
   
   //Option 1: Basic manual mapping
   var order = new Order();
   order.name = model.name;
   order.mrp = model.mrp;
   order.qty = qty;
   //If using EF, call your repository/context to add and save the order entity to DB.

   //Option 2: Use an auto mapper in this instance. I prefer express mapper.
   
// I say automapper because you have 2 objects with same properties. If your properties were different than you can still use express mapper but it has some additional configuration/setup needed to get it working.

   // Express mapper:http://www.expressmapper.org/
   Mapper.Register<order,>();
   var orderOpt2 = Mapper.Map<inventory,>(model);
   //Map additional properties specific to order
   orderOpt2.qty = qty;
   //Save to DB
}





现在我建议使用特定于此视图的模型,该模型包含此视图所需的所有字段/属性。您在问题中声明库存没有qty属性,您的视图强烈输入到库存实体,但您尝试对此实体上不存在的属性使用强类型编辑。 />


我在说什么,比如





Now i would suggest using a model specific to this view that encompasses all fields/properties necessary to this view. You stated in your question that Inventory doesn't have a property of qty, your view is strongly typed to the Inventory Entity, yet you are trying to use a strongly typed edit for a property on this Entity that doesn't exist.

What i'm saying do something like

public class InventoryModel
{
    public int Id {get;set;}
    public string name {get;set;}
    public string mrp {get;set;}
    public int qty {get;set;}
}

public class ProductController : Controller 
{
    public ActionResult Inventory(int invid)
    {
        var inventoryFromDb = //do db select here;
        var model = new InventoryModel(); // Similar to above, could use auto mapper or manually map. 
        model.Id = inventoryFromDb.id;
        model.name = inventoryFromDb.name;
        model.mrp = inventoryFromDb.mrp;
        //notice not doing anything here with qty.
        return View(model);
    }
}





然后在您的视图中,您将使用InventoryModel而不是SaveTextboxes.Models.inventory



Then in your view you would use InventoryModel instead of SaveTextboxes.Models.inventory


当您在将数据同时插入库存表时已有数量值时,您还可以将数量值插入订单表。在这种情况下,您不需要任何视图模型。
when you already have quantity value when you are inserting that data into inventory table at same time you can also insert quantity value into order table. you don't need any viewmodel in this scenario.


这篇关于从一个模型添加数据到另一个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆