从View到Controller MVC 4的POST List集合

时间:2016-10-04 08:30:50

标签: asp.net-mvc asp.net-mvc-4

在下面的问题上搜索了很多。最后决定与大家分享。  我有一个模特。

 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; }
    }

我将此模型绑定到MVC View。我的观点如下。

@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>

现在,当我在视图中填写所有详细信息并单击“提交”按钮时,我的控制器中的颜色为空。

所以,请帮助我如何在控制器上获取修改后的列表集合。

谢谢..

2 个答案:

答案 0 :(得分:0)

以下代码正常,请参阅下面的代码

型号:

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; }
    }

控制器

   [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);
        }

查看

@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" />

    }
}

答案 1 :(得分:0)

尝试使用IEnumerable并确保你的rcshtml文件没有违反mvc约定的规则(cshtml文件的名称对于该方法是相同的)

@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>
    }