8

I missed a little thing while passing a list from view to controller. It displays null in [HttpPost] method of controller. Anyone please guide that how can I get list data from view to controller. Please view my complete code below.

@model List<payorder_draft_printing.Models.registration>

@{
    ViewBag.Title = "bulk_approval";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">

    <div class="row" style="text-align: left">

        <h2><u>Bulk Approval</u></h2>

        <br />
        <br />

        @using (Html.BeginForm("bulk_approval", "Sms", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div style="width: 700px;" align="center">

                <table id="GetSerial" class="table">
                    <thead>
                        <tr class="ui-widget-header">
                            <th>Account Number</th>
                            <th>Mobile Number</th>
                            <th>Customer Name</th>
                            <th>Branch Code</th>
                            <th>Bulk Upload</th>
                            <th>Create Date</th>
                            <th>Created By</th>
                            <th>Active</th>
                           </tr>
                    </thead>

                    <tbody>

                        @if (Model != null)
                        {
                            foreach (var m in Model)
                            {
                            <tr style="height: 25px; border-bottom: 1px solid gray">
                                <td style="min-width: 120px">@m.account_number</td>
                                <td style="min-width: 120px; width: 450px;">@m.mobile_number</td>
                                <td style="min-width: 250px; width: 250px">@m.customer_name</td>
                                <td style="min-width: 100px; width: 100px">@m.BranchCode</td>
                                <td style="min-width: 100px; width: 100px">@m.BulkUpload</td>
                                <td style="min-width: 150px;">@string.Format("{0:dd-MMM-yyyy}", @m.create_date)</td>
                                <td style="min-width: 100px;">@m.created_by</td>
                                <td style="min-width: 100px; width: 100px">@m.Active</td>
                            </tr>
                            }
                        }

                    </tbody>

                </table>
                <input type="submit" value="Update" />
            </div>
        }

    </div>

</div>

In the following code i am trying to get the submitted list from view to controller but its result is null.

[HttpPost]
public ActionResult bulk_approval(List<registration> model)//here my model shows null, please guide.
{
    foreach (var abc in model)
    {

    }
    return View();
}

Following is my class.

public class registration
{
    public int Id { get; set; }
    public string mobile_number { get; set; }
    public string account_number { get; set; }
    public string customer_name { get; set; }
    public int FrequencyId { get; set; }
    public bool Active { get; set; }
    public string BranchCode { get; set; }
    public bool BulkUpload { get; set; }
    public string created_by { get; set; }
    public DateTime create_date { get; set; }
}
haldo
  • 14,512
  • 5
  • 46
  • 52
Rauf Abid
  • 313
  • 2
  • 8
  • 24
  • Use indexing (`for`-loop) instead of a `foreach` loop – devqon Sep 21 '16 at 11:24
  • Can you please share sample code, thanks. – Rauf Abid Sep 21 '16 at 11:27
  • Possible duplicate of [HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable) –  Sep 21 '16 at 11:31
  • Apert for the fact you cannot use a `foreach` loop (refer the dupe), your not creating any for controls to edit you data (making the form a bit pointless) –  Sep 21 '16 at 11:32

2 Answers2

14

With a foreach loop, MVC just creates the same inputs over and over again, because it doesn't know that they should be different. Therefore you need to use for-loops, and use indexes. I don't see any inputs in your form, so I'll just give you an example:

@if (Model != null) {
    for (var i = 0; i < Model.Count; i++) {
        @Html.TextBoxFor(m => Model[i].SomeProperty)
    }
}

Also, if I remember correctly, you need to use an IList as model:

@model IList<payorder_draft_printing.Models.registration>

To be able to post your (non-editable) texts, you need to add hidden inputs:

@Model[i].account_number 
@Html.HiddenFor(m => Model[i].account_number)
devqon
  • 13,818
  • 2
  • 30
  • 45
  • Thanks for your clarification Now iam getting values properly.Now How can i display values in the form of label instead of textbox, because you used @Html.TextBoxFor in your code, thanks – Rauf Abid Sep 21 '16 at 11:49
  • 2
    Why would you want to post stuff that aren't even editable? If you don't have inputs, the idea of a form fades – devqon Sep 21 '16 at 11:54
  • you are right, Actually my scenario is that i only need to be editable the Last column[Status] Boolean value either it is true or false, and rest of the columns should be just displaying as label, Please guide how to do it, thanks. – Rauf Abid Sep 21 '16 at 12:00
  • Same as always in MVC, but then with the indexing: `@Html.CheckBoxFor(m => m[i].Status)`. For the displaying you can just do `@m[i].account_number` – devqon Sep 21 '16 at 12:05
  • Thanks devqon, Sorry iam still not getting the desired thing by using [@m[i].account_number]. Object(@m) is not accessible because of removing foreach (var m in Model) line of code. Which makes it accessible to this object. In your proposed loop how can i make it accessible?, thanks – Rauf Abid Sep 21 '16 at 12:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123863/discussion-between-rauf-abid-and-devqon). – Rauf Abid Sep 21 '16 at 12:39
0

I know this is an old post, but i was stuck on this for several days and figured i would post my solution here. The first answer is mostly correct code, with one important difference. You cannot pass an IList, or List from view to controller in MVC.

As the other answer specifies, you cannot use a foreach and must use a for() loop to iterate through, referencing the indices with m => Model[i].account_number.

Secondly, and this is what had me stuck for a very long time, you cannot pass the IList<> or List. Create a new model in the back end that contains only a list of objects, for the example above perhaps you can name it accountNumberList. This model should contain a Public List of Account Number objects. After creation, modify the controller to pass the accountNumberList to the view, and bind the view to accountNumberList. Lastly, have the Post method accept the accountNumberList as the parameter. After that, you should be able to retrieve a List of Data from the view, where the user can modify multiple records in a single submit.

For anyone else that is as stuck as i was, i hope this helps.