发布到一个列表&LT; modeltype&GT; MVC3 [英] Posting to a list<modeltype> MVC3
问题描述
我试图让我的观点来发布列表返回动作但它一直在为空来了。
I am trying to get my view to post a List back to the action however it keeps coming in as null.
所以我的模型有WeightEntry对象的列表。
So my Model has a List of WeightEntry objects.
锻炼方式
public class Exercise
{
public List<WeightEntry> Entries { get; set; }
public int ExerciseID { get; set; }
public int ExerciseName { get; set; }
}
WeightEntry型号
WeightEntry Model
public class WeightEntry
{
public int ID { get; set; }
public int Weight { get; set; }
public int Repetition { get; set; }
}
我查看包含ExerciseName和WeightEntry对象的forloop
My View contains the ExerciseName and a forloop of WeightEntry objects
@model Mymvc.ViewModels.Exercise
...
<span>@Model.ExerciseName</span>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table class="left weight-record">
<tr>
<th>Reps</th>
<th>Weight</th>
</tr>
@foreach (var item in Model.Entries)
{
<tr>
<td>
@Html.EditorFor(x => item.Repetition)
</td>
<td>
@Html.EditorFor(x => item.Weight)
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
}
控制器动作(POST)不执行任何操作的时刻。我只是试图让绑定工作之前,我加上节省code。
The Controller Action (Post) Does nothing at the moment. I am just trying to get the binding working before I add the save code.
[HttpPost]
public ActionResult WeightEntry(Exercise exercise)
{
try
{
//Add code here to save and check isvalid
return View(exercise);
}
catch
{
return View(exercise);
}
}
我已经看到了添加分子在MVC2使用的表单元素的名字了一些小动作,但我不知道是否是MVC3有什么不同?我希望它会所有的ID为0或空的很好结合,而是整个列表为空时,我的形式帖子后检验。任何帮助是AP preciated。
谢谢你。
I have seen a few little tricks with adding a numerator to the form elements' names used in MVC2 but I was wondering if MVC3 was any different? I was hoping it would all bind nicely with ID's being 0 or null but instead the whole List is null when I inspect it after the form posts. Any help is appreciated. Thanks.
推荐答案
替换下面的循环:
@foreach (var item in Model.Entries)
{
<tr>
<td>
@Html.EditorFor(x => item.Repetition)
</td>
<td>
@Html.EditorFor(x => item.Weight)
</td>
</tr>
}
与
@for (var i = 0; i < Model.Entries.Count; i++)
{
<tr>
<td>
@Html.EditorFor(x => x.Entries[i].Repetition)
</td>
<td>
@Html.EditorFor(x => x.Entries[i].Weight)
</td>
</tr>
}
甚至更好,使用的编辑器模板和替换循环:
or even better, use editor templates and replace the loop with:
@Html.EditorFor(x => x.Entries)
和再定义,它会自动呈现的条目集合中的每个元素(〜/查看/共享/ EditorTemplates / WeightEntry.cshtml
)的自定义编辑模板:
and then define a custom editor template that will automatically be rendered for each element of the Entries collection (~/Views/Shared/EditorTemplates/WeightEntry.cshtml
):
@model WeightEntry
<tr>
<td>
@Html.EditorFor(x => x.Repetition)
</td>
<td>
@Html.EditorFor(x => x.Weight)
</td>
</tr>
在生成的输入元素将有<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">correct 的名字,你将能够成功地获取它们放回你的POST操作。
The the generated input elements will have correct names and you will be able to successfully fetch them back in your POST action.
这篇关于发布到一个列表&LT; modeltype&GT; MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!