简体   繁体   中英

Looping list of object inside partial view returning null values in post

I have a partial view inside which loops my Availability object for getting values from text box. I have added my code in here.

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

My controller class:

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

Here, list count will vary. Can be more that 10.

My main view:

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

My partial view:

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

My error is - When I click on submit to post data to ListOfPost() ActionResult, parameter aaa is returning null value. I doubt that the cause of issue is name will be save for all text box.

How can I get user entered values in post???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM