MVC3剃须刀"For"模型-内容重复 [英] MVC3 Razor "For" model - contents duplicated

查看:75
本文介绍了MVC3剃须刀"For"模型-内容重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管正确地从服务器接收了数据,但我的MVC3剃刀形式仍会在 foreach 代码块内呈现重复的值,这很有趣.这是我在MVC3 Razor中使用的简单表格...

-我的.cshtml页面示例

  @model列表<类别>@using(@ Html.BeginForm(保存",类别",FormMethod.Post)){foreach(模型中的类别猫){< span>测试:@ cat.CategoryName</span>< span>实际:@ Html.TextBoxFor(model => cat.CategoryName)</span>@ Html.HiddenFor(模型=>目录ID)< p> ---</p>}<输入type ="submit" value =保存" name ="btnSaveCategory" id ="btnSaveCategory"/>} 

我的控制器动作看起来像这样-

  [HttpPost]公共ActionResult保存(ViewModel.CategoryForm cat){...根据发布的"cat"值保存数据(我在这里正确接收了它们)列表<类别>cL = ...在此处填充类别列表返回View(cL);} 

上面的save操作返回具有正确数据的模型.

提交上面的表格后,我希望在完成操作后看到类似于以下内容的类别值...

 测试:Category1,实际:Category1测试:类别2,实际:类别2测试:类别3,实际:类别3测试:类别4,实际:类别4 

但是 @ Html.TextBoxFor 复制列表中的第一个值.发布表单后,我看到如下响应.即使我从服务器获得了正确的数据,也会重复实际"值.

 测试:Category1,实际:Category1测试:类别2,实际:类别1测试:类别3,实际:类别1测试:类别4,实际:类别1 

我做错了什么?任何帮助将不胜感激.

解决方案

TextBoxFor 这样的帮助器方法旨在与ViewModel一起使用,该ViewModel表示单个对象,而不是对象的集合./p>

正常使用是:

  @ Html.TextBoxFor(c => c.Name) 

在方法内部将 c 映射到 ViewData.Model 的地方.

您正在做不同的事情:

  @ Html.TextBoxFor(c => erationItem.Name) 

方法internall仍将尝试使用ViewData.Model作为呈现的基础对象,但是您打算在迭代项上使用它.该语法对编译器有效,但可以解决此问题.

一种解决方法是制作一个可在单个项目上运行的局部视图:在该视图内,您可以使用语法正确的html帮助器(第一个示例),然后在foreach内调用它,将迭代项作为参数传递.那应该可以正常工作.

It has been intriguing that my MVC3 razor form renders duplicated values inside a foreach code block in spite of correctly receiving the data from the server. Here is my simple form in MVC3 Razor...

-- sample of my .cshtml page

@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" />
}

My controller action looks something like this -

 [HttpPost]
    public ActionResult Save(ViewModel.CategoryForm cat)
    {
       ... save the data based on posted "cat" values (I correctly receive them here)

       List<Category> cL = ... populate category list here
       return View(cL);
    }

The save action above returns the model with correct data.

After submitting the form above, I expect to see values for categories similar to the following upon completing the action...

Test: Category1, Actual:Category1
Test: Category2, Actual:Category2
Test: Category3, Actual:Category3
Test: Category4, Actual:Category4

However @Html.TextBoxFor duplicates the first value from the list. After posting the form, I see the response something like below. The "Actual" values are repeated even though I get the correct data from the server.

Test: Category1, Actual:Category1
Test: Category2, Actual:Category1
Test: Category3, Actual:Category1
Test: Category4, Actual:Category1

What am I doing wrong? Any help will be appreciated.

解决方案

The helper methods like TextBoxFor are meant to be used with a ViewModel that represent the single object, not a collection of objects.

A normal use would be:

@Html.TextBoxFor(c => c.Name)

Where c gets mapped, inside the method, to ViewData.Model.

You are doing something different:

@Html.TextBoxFor(c => iterationItem.Name)

The method internall will still try to use the ViewData.Model as base object for the rendering, but you intend to use it on the iteration item. That syntax, while valid for the compiler, nets you this problem.

A workaround is to make a partial view that operates on a single item: inside that view you can use html helpers with correct syntax (first sample), and then call it inside the foreach, passing the iteration item as parameter. That should work correctly.

这篇关于MVC3剃须刀"For"模型-内容重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆