Asp.net mvc 3 MVC3剃须刀“;至于;模型-重复的内容
有趣的是,我的MVC3 razor表单在Asp.net mvc 3 MVC3剃须刀“;至于;模型-重复的内容,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,有趣的是,我的MVC3 razor表单在foreach代码块中呈现重复的值,尽管正确地从服务器接收数据。这是我在MVC3 Razor中的简单表单 --my.cshtml页面的示例 @model List<Category> @using (@Html.BeginForm("Save", "Categories", FormMethod.Post)) { foreach (Category cat in Model) { <span>Test: @cat.
foreach
代码块中呈现重复的值,尽管正确地从服务器接收数据。这是我在MVC3 Razor中的简单表单
--my.cshtml页面的示例
@model List<Category>
@using (@Html.BeginForm("Save", "Categories", FormMethod.Post))
{
foreach (Category cat in Model)
{
<span>Test: @cat.CategoryName</span>
<span>Actual: @Html.TextBoxFor(model => cat.CategoryName)</span>
@Html.HiddenFor(model => cat.ID)
<p>---</p>
}
<input type="submit" value="Save" name="btnSaveCategory" id="btnSaveCategory" />
}
但是@Html.TextBoxFor
会复制列表中的第一个值。在发布表单后,我看到了如下的回复。即使我从服务器获得了正确的数据,“实际”值也会重复
Test: Category1, Actual:Category1
Test: Category2, Actual:Category1
Test: Category3, Actual:Category1
Test: Category4, Actual:Category1
我做错了什么?任何帮助都将不胜感激。像
TextBoxFor
这样的帮助方法用于表示单个对象而不是对象集合的ViewModel
通常的用途是:
@Html.TextBoxFor(c => c.Name)
其中c
在方法内部映射到ViewData.Model
您正在做一些不同的事情:
@Html.TextBoxFor(c => iterationItem.Name)
internall方法仍将尝试使用ViewData.Model作为渲染的基本对象,但您打算在迭代项上使用它。该语法虽然对编译器有效,但可以解决此问题
解决方法是创建一个对单个项进行操作的局部视图:在该视图中,您可以使用语法正确的html帮助程序(第一个示例),然后在foreach中调用它,并将迭代项作为参数传递。这应该可以正常工作。更好的方法是使用EditorTemplates 在您的表格中,您可以这样做:
@model List<Category>
@using (@Html.BeginForm("Save", "Categories", FormMethod.Post))
{
@Html.EditorForModel()
<input type="submit" value="Save" name="btnSaveCategory" id="btnSaveCategory" />
}
@型号列表
@使用(@Html.BeginForm(“保存”、“类别”、FormMethod.Post))
{
@Html.EditorForModel()
}
然后,您将在~/Views/Shared文件夹或控制器视图文件夹中创建一个名为EditorTemplates的文件夹(取决于您是希望与整个应用程序共享模板还是仅此控制器共享模板),并在EditorTemplates文件夹中创建一个Category.cshtml文件,如下所示:
@model Category
<span>Test: @Model.CategoryName</span>
<span>Actual: @Html.TextBoxFor(model => model.CategoryName)</span>
@Html.HiddenFor(model => model.ID)
<p>---</p>
@模型类别
测试:@Model.CategoryName
实际:@Html.TextBoxFor(model=>model.CategoryName)
@Html.HiddenFor(model=>model.ID)
---
MVC将自动迭代集合并为其中的每个项目调用模板。我注意到,在视图中使用foreach循环会导致集合中每个项目的文本框的名称属性呈现相同。对于您的示例,每个文本框都将使用以下ID和名称属性呈现:
<input id="cat_CategoryName" name="cat.CategoryName" value="Category1" type="text">
控制器
public ActionResult Index()
{
// create the view model with some test data
CategoryForm form = new CategoryForm()
{
Categories = new List<Category>()
};
form.Categories.Add(new Category() { ID = 1, CategoryName = "Category1" });
form.Categories.Add(new Category() { ID = 2, CategoryName = "Category2" });
form.Categories.Add(new Category() { ID = 3, CategoryName = "Category3" });
form.Categories.Add(new Category() { ID = 4, CategoryName = "Category4" });
// pass the CategoryForm view model
return View(form);
}
[HttpPost]
public ActionResult Save(CategoryForm cat)
{
// the view model will now have the correct categories
List<Category> cl = new List<Category>(cat.Categories);
return View("Index", cat);
}
public ActionResult Index()
{
//使用一些测试数据创建视图模型
CategoryForm form=新的CategoryForm()
{
类别=新列表()
};
Add(new Category(){ID=1,CategoryName=“Category1”});
Add(newcategory(){ID=2,CategoryName=“Category2”});
Add(new Category(){ID=3,CategoryName=“Category3”});
Add(new Category(){ID=4,CategoryName=“Category4”});
//传递类别窗体视图模型
返回视图(表格);
}
[HttpPost]
公共行动结果保存(类别格式cat)
{
//视图模型现在将具有正确的类别
列表cl=新列表(类别);
返回视图(“索引”,cat);
}
<input id="cat_CategoryName" name="cat.CategoryName" value="Category1" type="text">
@model CategoryForm
@using TestMvc3.Models
@using (@Html.BeginForm("Save", "Categories", FormMethod.Post))
{
for (int i = 0; i < Model.Categories.Count; i++)
{
<span>Test: @Model.Categories[i].CategoryName</span>
<span>Actual: @Html.TextBoxFor(model => Model.Categories[i].CategoryName)</span>
@Html.HiddenFor(model => Model.Categories[i].ID)
<p>---</p>
}
<input type="submit" value="Save" name="btnSaveCategory" id="btnSaveCategory" />
}
public ActionResult Index()
{
// create the view model with some test data
CategoryForm form = new CategoryForm()
{
Categories = new List<Category>()
};
form.Categories.Add(new Category() { ID = 1, CategoryName = "Category1" });
form.Categories.Add(new Category() { ID = 2, CategoryName = "Category2" });
form.Categories.Add(new Category() { ID = 3, CategoryName = "Category3" });
form.Categories.Add(new Category() { ID = 4, CategoryName = "Category4" });
// pass the CategoryForm view model
return View(form);
}
[HttpPost]
public ActionResult Save(CategoryForm cat)
{
// the view model will now have the correct categories
List<Category> cl = new List<Category>(cat.Categories);
return View("Index", cat);
}