Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a partial view inside which loops my Availability object for getting values from text box. I have added my code in here.

My model class:
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:
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(List<WebApplication1.Class.Availability> aaa)
      {

          return View();
      }
  }


My main view:

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

My partail view:
<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.TextBoxFor(modelItem => item.Name, new { @class = "Name" })
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.Address, new { @class = "Address" })
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.Gender, new { @class = "Gender" })
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.Adult, new { @class = "Adult" })
                </td>
                <td>
                    @Html.TextBoxFor(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???

What I have tried:

To differentiate name, I used below code.

@{int i = 0;      }
  @foreach (var item in Model)
  {
      <tr>
          <td>
              @Html.TextBoxFor(modelItem => item.Name, new { Name = item.Name + i, id = item.Name + i })
          </td>
          <td>
              @Html.TextBoxFor(modelItem => item.Address, new { Name = item.Address + i, id = item.Address + i })
          </td>
          <td>
              @Html.TextBoxFor(modelItem => item.Gender, new { Name = item.Gender + i, id = item.Gender + i })
          </td>
          <td>
              @Html.TextBoxFor(modelItem => item.Adult, new { Name = item.Adult + i, id = item.Adult + i })
          </td>
          <td>
              @Html.TextBoxFor(modelItem => item.Child, new { Name = item.Child + i, id = item.Child + i })
          </td>
      </tr>
      @{  i += 1; }*@
Posted
Updated 14-Jul-21 22:56pm

1 solution

You can't use "foreach" in the view like that, use a "for" loop instead

razor - ASP.NET MVC 4 - for loop posts model collection properties but foreach does not - Stack Overflow[^]

Your model on the partial view is IEnumerable which doesn't support what you need. Change it to

@model List<WebApplication1.Class.Availability>


Now change the form to

@for (int i=0; i < Model.Count; i++)
{
    <tr>
        <td>
            @Html.TextBoxFor(modelItem => Model[i].Name, new { @class = "Name" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model[i].Address, new { @class = "Address" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model[i].Gender, new { @class = "Gender" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model[i].Adult, new { @class = "Adult" })
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model[i].Child, new { @class = "Child" })
        </td>
    </tr>
}


You also have an issue with your headings. As long as there is at least one item in the model collection you can do this

<tr>
    <th>
        @Html.DisplayNameFor(model => model.First().Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.First().Address)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.First().Gender)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.First().Adult)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.First().Child)
    </th>
</tr>


However if you can't guarantee there will be at least one item you should find another way of passing the field titles to the view.
 
Share this answer
 
v2
Comments
Codes DeCodes 19-Mar-19 8:39am    
I used this as shown in the link.. Still getting null.
Please let me know if I am doing some mistake.

@for (var i = 0; i < Model.Count(); i++)
{
@Html.TextBoxFor(m => Model.ElementAt(i).Name) @Html.TextBoxFor(m => Model.ElementAt(i).Address) @Html.TextBoxFor(m => Model.ElementAt(i).Gender) @Html.TextBoxFor(m => Model.ElementAt(i).Adult) @Html.TextBoxFor(m => Model.ElementAt(i).Child)

}
F-ES Sitecore 19-Mar-19 8:57am    
I've updated my solution
Codes DeCodes 22-Mar-19 2:07am    
Exactly what I wanted. Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900