0

I have created a MVC 5 application where I am creating a custom purchasing system.

My View model is generated based on the contents of an SQL Database. The database uses a Single Primary Key for a Purchase Order with some basic data. In addition to the basic data each PO can have multiple items within that purchase order. This creates a relationship where there can be N items to a single Purchase.

When I am doing a form Submit the sending of the Purchase Order specific information, ie using TextBoxFor(m => m.Date), works fine. However I cannot seem to pass the list of all the Items within it. For example when I am doing @Html.TextBoxFor(m => m.Details.ItemName) it is not populating the section in the model during the POST.

I have tried sending all the information using @Html.TextBoxFor(m => m.Details) but it throws errors as the TextBoxFor cannot handle these data types.

I am after a system where I can edit a purchase order model by adding or removing items without committing to my database until the 'save' button is pressed.

My View Model example is

public class PurchasingEditModel
{
    public tbl_PurchaseOrder PurchaseOrder;
}

Purchase order in itself has a link to PurchaseOrderDetails (within SQL) via a Foreign key reference of the Purchase Order. This creates the Many Items to One PO.

The controller for the edit page is

public ActionResult Edit(int? pkPurchaseOrder)
{
 model.PurchaseOrder = db.PurchaseOrder.Where(x => x.pkPurchaseOrder == pkPurchaseOrder);

return View(model);
}

This together with a standard Edit.cshtml using @Html.TextBoxFor(m => m. all works fine with the display portion, until I want to post the multiple items within the sub-table.

Really the easiest way for me to handle this would be to pass the all contents of model during the display, which has all the elements of the items within the PO, to the POST function.

Any guidance you can give is appreciated.

Tom
  • 1
  • 2
  • Try the mighty `@Html.EditorFor...`, it can generate HTML which is able to post back more complex types and even lists. With the help of `EditorTemplates`, you can control which Items should be displayed and which should be rendered as hidden fields. In the end, you'll need to send everything back to the page. Some help: https://mhwelander.net/2014/03/26/asp-net-mvc-model-binding-not-occurring-when-posting-list-of-complex-types/ – thmshd Mar 22 '17 at 15:26
  • 1
    @thomashaid.com, `EditorFor()` will not allow dynamically adding and deleting collection items. –  Mar 22 '17 at 21:28
  • Refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for some examples –  Mar 22 '17 at 21:30
  • @StephenMuecke oh, yes of course, I somehow missed that requirement, it seemed to me like Tom had much more basic issues (e.g. not being able to send the complex object at all). Anyway, for that "dynamic" approach, something like http://knockoutjs.com/ will give you the most comfort. I wish the integration with ASP.NET would be more seamless though. (haven't tried http://knockoutmvc.com/ yet) – thmshd Mar 23 '17 at 06:04
  • Unfortunately I am bound to using ASP.net...... I have tried many different methods of trying to pass the tables back. Its fustrating that MVC has no way of just passing ALL the model data to the POST function. If that were the case it would be extremely easy to do what I need to. – Tom Mar 23 '17 at 07:23
  • @Tom the solutions I listed above, especially knockoutmvc.com, work for ASP.NET MVC but add an extra JavaScript layer for dynamic handling in the UI. The, say, "*limitations*" of ASP.NET MVC are solved by serializing the Model to and from JSON – thmshd Mar 23 '17 at 11:20

1 Answers1

0

I have solved my own issue, which makes a change...

To POST the data within a sub-table(IQueryable) Ive had to do the following

Within my main html code add

@foreach(var item in Model.PurchaseOrder.tbl_Details)
{
    @Html.Partial("_POItemDetailsPartial", item)
}

Then Ive had to create a MVC Partial Page (Razor) named _POItemsPartial.

Within that page I have put the following..

@using (Html.BeginCollectionItem("Model.PurcahseOrder.tbl_Details")
{
    @model ModelSpace.Model.tbl_Details

    @Html.TextBoxFor(m => m.Description)
}

When the POST action occurs I now get the description for all the 3 items I have within the PurchaseOrder.

**

UPDATE:

**

Despite getting this data..I am now losing all my original model data such as Total Price it seems that the Partial Call being passed the Model.tbl_Details is only passing that information and clearing the rest. When it returns it ends up with all higher level data removed. If I comment out the partial call it all comes back. Turns out I still need help.... :(

Tom
  • 1
  • 2