-1

After searching a lot on below issue. Finally decided to share with you. I have one model.

 public class EarlyBirdWeb
    {
       public string  Client { get; set; }
       [Display(Name = "Job Name")]
       public string JobName { get; set; }
       public List<SelectListItem> Reasons { get; set; }
       public List<Status> status { get; set; }
       public List<ETA> etas { get; set; } 
       [Display(Name="Call BU")]
       public string CallBU { get; set; }
    }

I am binding this model to MVC View. My view as follows.

@model List<EarlyBird.Models.EarlyBirdWeb>

<form method="post">
    @Html.AntiForgeryToken()  

    <div class="row">
        <table class="table table-bordered table-inverse">
            <tr class="bg-warning">
                <th>Client
                </th>
                <th>Job Name
                </th>
                <th>Reason</th>
                <th>Status</th>
                <th>ETA</th>
                <th>CallBU?
                </th>

            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                      @item.Client
                      @Html.Hidden(@item.Client)
                    </td>
                    <td>
                        @item.JobName
                    </td>
                    <td>
                        @Html.DropDownList("--Select One--", (IEnumerable<SelectListItem>)@item.Reasons)

                    </td>
                    <td>
                        <select>
                            @foreach (var status in item.status)
                            {
                                <option value="@status.StatusName">@status.StatusName</option>           
                            }
                        </select>
                    </td>
                    <td>
                        <select>
                            @foreach (var etas in item.etas)
                            {
                                <option value="@etas.ETATime">@etas.ETATime</option>           
                            }
                        </select>
                    </td>
                    <td>
                        @item.CallBU
                    </td>
                </tr>
            }

        </table>

    </div>
    <div class="row">
        <input type="submit" value="Submit" name="Submit" class="btn btn-warning" />
    </div>
</form>

Now, when I fill all details in view and click on submit button, I am getting null in my controller.

So, please assist me how I can get my modified list collection on controller.

Thanks..

user2493287
  • 255
  • 1
  • 4
  • 16
  • Possible duplicate of [Post an HTML Table to ADO.NET DataTable](http://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable) –  Oct 04 '16 at 10:22
  • You cannot use a `foreach` loop to generate form controls for collection iteme (refer the dupe) –  Oct 04 '16 at 10:23

2 Answers2

0

Following code works fine,See the code below

Model :

public class EarlyBirdWeb
    {
       public string  Client { get; set; }
       [Display(Name = "Job Name")]
       public string JobName { get; set; }
       public List<SelectListItem> Reasons { get; set; }
       public List<Status> status { get; set; }
       public List<ETA> etas { get; set; } 
       [Display(Name="Call BU")]
       public string CallBU { get; set; }
    }

Controller

   [HttpPost]
   public ActionResult Sample(EarlyBirdWeb model)
        {
            if (ModelState.IsValid)
            {
                  // do your stuff like: save to database and redirect to required page.
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

View

@model MvcForums.Models.EarlyBirdWeb

@{
    using (Html.BeginForm("Sample", "ControllerName", FormMethod.Post))
    {
        <label>Client</label>
        @Html.TextBoxFor(m => m.Client)

        <br/>

        <label>JobName</label>
        @Html.TextBoxFor(m => m.JobName)

        <input type="submit" value="btnSubmit" />

    }
}
  • Thanks for reply. But my view going to return collection of EarlyBirdWeb and your solution for only one record. Could you please suggest something else. – user2493287 Oct 04 '16 at 09:01
0

try to use IEnumerable and make sure that you rcshtml file does not break the law of mvc convention (the name of the cshtml file is the same for the method )

@model IEnumerable<EarlyBird.Models.EarlyBirdWeb>

     @using (Html.BeginForm())
                {
        @Html.AntiForgeryToken()  

        <div class="row">
            <table class="table table-bordered table-inverse">
                <tr class="bg-warning">
                    <th>Client
                    </th>
                    <th>Job Name
                    </th>
                    <th>Reason</th>
                    <th>Status</th>
                    <th>ETA</th>
                    <th>CallBU?
                    </th>

                </tr>

                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                          @item.Client
                          @Html.Hidden(@item.Client)
                        </td>
                        <td>
                            @item.JobName
                        </td>
                        <td>
                            @Html.DropDownList("--Select One--", (IEnumerable<SelectListItem>)@item.Reasons)

                        </td>
                        <td>
                            <select>
                                @foreach (var status in item.status)
                                {
                                    <option value="@status.StatusName">@status.StatusName</option>           
                                }
                            </select>
                        </td>
                        <td>
                            <select>
                                @foreach (var etas in item.etas)
                                {
                                    <option value="@etas.ETATime">@etas.ETATime</option>           
                                }
                            </select>
                        </td>
                        <td>
                            @item.CallBU
                        </td>
                    </tr>
                }

            </table>

        </div>
        <div class="row">
            <input type="submit" value="Submit" name="Submit" class="btn btn-warning" />
        </div>
    }