部分视图中的对象的循环列表在发布时返回空值

时间:2019-03-19 12:45:57

标签: c# asp.net-mvc post razor

我内部有一个局部视图,该视图循环我的Availability对象以从文本框中获取值。我已经在这里添加了代码。

public class Availability
    {
        public int ID { get; set; }
        public string Name{ get; set; }
        public string Address { get; set; }
        public string Gender { get; set; }
        public int Adult { get; set; }
        public int Child { get; set; }
    }

我的控制器类:

public class HomeController : Controller
    {
        List<Availability> lst = new List<Availability>();

        public ActionResult Index()
        {
            Availability aa = new Availability();
            Availability a1 = new Availability();
            Availability a2 = new Availability();
            Availability a3 = new Availability();
            lst.Add(aa);
            lst.Add(a1);
            lst.Add(a2);
            lst.Add(a3);

            return View(lst);
        }
        public ActionResult ListOfPost(IEnumerable<WebApplication1.Class.Availability> aaa)
        {

            return View();
        }
    }

此处,列表计数会有所不同。可以超过10。

我的主视图:

@model List<WebApplication1.Class.Availability>
@{
    ViewBag.Title = "Home Page";
}

<script type="text/javascript" src="http://code.jquery.com/jquery-3.3.1.js"></script>
    Index page
@Html.Partial("_Availability", Model)

我的局部视图:

@model IEnumerable<WebApplication1.Class.Availability>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("ListOfPost", "Home", FormMethod.Post))
{

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Gender)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Adult)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Child)
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.EditorFor(modelItem => item.Name, new { @class = "Name" })
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.Address, new { @class = "Address" })
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.Gender, new { @class = "Gender" })
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.Adult, new { @class = "Adult" })
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.Child, new { @class = "Child" })
                </td>
            </tr>
        }
    </table>
    <input id="btnSubmit" class="btn btn-default" type="submit" value="Submit">
}

我的错误是-单击提交将数据发布到ListOfPost()ActionResult时,参数aaa返回空值。我怀疑问题的原因是名称是否会保存在所有文本框中。

如何在帖子中获取用户输入的值?

1 个答案:

答案 0 :(得分:0)

您需要一个拥有IEnumerable<Availability> availabilities属性的viewmodel类,并将其返回到视图中。

相关问题