作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让我的观点将列表发布回操作,但它一直为空。
所以我的模型有一个 WeightEntry 对象列表。
运动模型
public class Exercise
{
public List<WeightEntry> Entries { get; set; }
public int ExerciseID { get; set; }
public int ExerciseName { get; set; }
}
public class WeightEntry
{
public int ID { get; set; }
public int Weight { get; set; }
public int Repetition { get; set; }
}
@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" />
}
[HttpPost]
public ActionResult WeightEntry(Exercise exercise)
{
try
{
//Add code here to save and check isvalid
return View(exercise);
}
catch
{
return View(exercise);
}
}
最佳答案
替换以下循环:
@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>
}
@Html.EditorFor(x => x.Entries)
~/Views/Shared/EditorTemplates/WeightEntry.cshtml
):
@model WeightEntry
<tr>
<td>
@Html.EditorFor(x => x.Repetition)
</td>
<td>
@Html.EditorFor(x => x.Weight)
</td>
</tr>
关于asp.net-mvc-3 - 发布到列表<modeltype> MVC3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306246/
我有一个 MVC 3 应用程序和一个带有一行的 View : @ModelType String 这会导致 Index.vbhtml 中出现“语法错误”,但未指定行或列。研究表明 Razor View
我试图让我的观点将列表发布回操作,但它一直为空。 所以我的模型有一个 WeightEntry 对象列表。 运动模型 public class Exercise { public List En
我正在尝试使用 asp.net 4.5 的模型绑定(bind)功能。我正在使用 Repeater 控件,并在其上设置 ModelType 属性,但是一旦运行应用程序,我就会收到解析器错误,该错误指出
我最近对我的两个项目进行了 NuGet 包更新。所以我使用的是 Microsoft.AspNet.Mvc 5.2.0 版。一个项目运行良好,另一个项目出现问题,因为我现在无法创建强类型 Razor V
我是一名优秀的程序员,十分优秀!