0

I've read through many reported issues relating to this, none have resolved the problem I'm having.

Model:

public class MySoftwareResults
{
    public Shopping_MachineInformation MachineInformation { get; set; }
    public ShoppingUserInformation UserInformation { get; set; }
    public List<Shopping_MySoftwareResults> ApplicationsList { get; set; }
    public string Requester { get; set; }

    public MySoftwareResults()
    {
        MachineInformation = new Shopping_MachineInformation();
        UserInformation = new ShoppingUserInformation();
        ApplicationsList = new List<Shopping_MySoftwareResults>();
        Requester = "";
    }
}

Form:

@using (@Html.BeginForm("MySoftwareResults", "Client", FormMethod.Post))
    {
        <div class="form-group">
            <table class="table table-responsive list-view">
                <thead>
                <tr>
                    <th>Software</th>
                    <th>Cost</th>
                    <th>Requires Approval</th>
                    <th>Status</th>
                    <th>Select</th>
                </tr>
                </thead>
                <tbody>
                @foreach (var item in Model.ApplicationsList)
                {
                    <tr>
                        <td>
                            @Html.LabelForModel(item.Software)
                        </td>
                        <td>@Html.LabelForModel(item.Cost)</td>
                        <td>
                            @Html.LabelForModel(item.RequiresApproval)
                        </td>
                        <td>@Html.LabelForModel(item.Status)</td>
                        <td>
                            <input type="checkbox" id="Selected" name="Selected" value="@item.CollectionID"/>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
        <div class="form-group">
            <input type="submit" title="SUBMIT" class="btn btn-primary pull-right" id="butSubmit" />
        </div>
    }

The form populates perfectly. When I click on Submit the Model is empty:

[HttpPost]
public ActionResult MySoftwareResults(MySoftwareResults results)
{            
    var selected = axp.euc.sdsassistance.core.Queries.Shopping_ParseCheckedItems(Request.Form["Selected"]);...
}

I tried using Fiddler, but I can't find anything to reflect the model data being passes when the form loads.

I'm stumped.

adiga
  • 34,372
  • 9
  • 61
  • 83
Edmound
  • 19
  • 3
  • Pretty sure you need to give your values the 'name' attribute before you can use them through Request.Form[]. Unsure if this works with the built-in Html.LabelForModel. – DGK Feb 02 '17 at 17:08
  • Don't think that's it. Originally I was resolving an issue with checkboxes. I've gotten that figured out and the Request.Form[] contains that data. In that example I built to resolve the checkbox issue the Model binded to the post. But in my application it's not. – Edmound Feb 02 '17 at 18:36
  • Your generating html which has no relationship to your model. The only input your generate has `name="Selected"` and that would only bind to `public ActionResult MySoftwareResults(bool[] selected)` which would be useless. Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how to bind to collections. And forms only send back the name/value pairs of successful controls (``, ` –  Feb 02 '17 at 21:51
  • Ah! Duh! I'm closer to getting this to bind. I've managed to get the ApplicationList to bind. How would I get the other sub-models to bind? I don't want to display the data, just want it to transfer over to the controller? I tried @Html.HiddenFor(x=>x.MachineInformation), but that didn't work. – Edmound Feb 02 '17 at 22:56

1 Answers1

0

Thanks for Stephen Muecke for directing me to better understanding how the modeling works in forms. I was able to bind my data over to the POST call by doing the following.

For the models I wasn't displaying data for, but needed them to be bind to the POST I did:

<div style="display: none">
            @Html.EditorFor(x=>x.MachineInformation)
            @Html.EditorFor(x=>x.UserInformation)
        </div>

For the Model I wanted to display data for I did:

@for (int i = 0; i < Model.ApplicationsList.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.LabelFor(m => m.ApplicationsList[i].Software, new {@class = "form-controller"})
                        </td>
                        <td>@Html.LabelFor(m => m.ApplicationsList[i].Cost, new {@class = "form-controller"})</td>
                        <td>
                            @Html.LabelFor(m => m.ApplicationsList[i].RequiresApproval, new {@class = "form-controller"})
                        </td>
                        <td>@Html.LabelFor(m => m.ApplicationsList[i].Status, new {@class = "form-controller"})</td>
                        <td>
                            <input type="checkbox" id="Selected" name="Selected" value="@Model.ApplicationsList[i].CollectionID"/>
                        </td>
                    </tr>
                }

When I clicked on Submit my Model populated in the controller!!

Edmound
  • 19
  • 3