Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>
Posted
Updated 4-Oct-16 2:05am

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.

C#
[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
}


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

C#
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);
    }
}


Then in your view you would use InventoryModel instead of SaveTextboxes.Models.inventory
 
Share this answer
 
v2
Comments
mayank.bhuvnesh 29-Aug-16 9:21am    
Why I ave to create InventoryModel like above as I already have Inventory and Order table. The only difference is Inventory table dont have Qty field. Is there a requirement to create an Intermidiate table or class???
David_Wimbley 29-Aug-16 15:12pm    
There is no requirement to use a view model. You can do anything you want. I'm merely stating you are trying to use a property that you say doesn't exist on your inventory class in a strongly typed manner.

In your case you are trying to make your view do something that your model, Inventory, cannot do...which is use the property qty in a strongly typed manner.

View models are specific to the view itself and therefore typically not reusable, however if there is a need for the view to do specific things/contain properties that may not be accommodated by the model, then a view model is a possible solution.

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900