在局部视图内循环对象列表,在post中返回null值 [英] 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.
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">
}
我的错误是 - 当我点击提交将数据发布到ListOfPost()ActionResult时,参数aaa返回空值。我怀疑问题的原因是名称将保存为所有文本框。
如何在帖子中输入用户输入的值???
我尝试了什么:
为了区分名称,我使用了下面的代码。 />
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; }*@
推荐答案
你不能在视图中使用foreach,使用for循环代替
razor - ASP.NET MVC 4 - for循环发布模型集合属性但foreach没有 - Stack Overflow [ ^ ]
您在局部视图上的模型是IEnumerable,它不支持您所需的内容。将其更改为
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.
这篇关于在局部视图内循环对象列表,在post中返回null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!