.NET MVC3 Html.HiddenFor foreach循环预期的集合里面呢? [英] .NET MVC3 Html.HiddenFor inside of foreach loop expecting a collection?

查看:91
本文介绍了.NET MVC3 Html.HiddenFor foreach循环预期的集合里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是部分的一部分:

@model IEnumerable<BLL.DomainModel.Jerk>

@foreach (var jerk in Model)
{
    using (Html.BeginForm("AddJerk", "Jerk", FormMethod.Post, new { @class = "jerkListForm" }))
    {
     @Html.HiddenFor(jerk => )
       @jerk.Name
       ...  
    }
}

该类型的 HiddenFor 拉姆达正在寻找的是一样的 @model (IEnumerable的),而我在寻找内的IEnumerable的单个对象。

The type that the HiddenFor lambda is looking for is the same as the @model (IEnumerable), whereas I'm looking for a single object within that IEnumerable.

我在想什么?为什么它仍然foreach循环内寻找一个集合?

What am I missing? Why is it still looking for a collection inside of the foreach loop?

推荐答案

所有HTML对于由Asp.Net MVC提供的助手使用页面的定义模型的模型。你可以做的就是创建一个 EditorTemplate 。创建你的视图文件夹中的子目录名为 EditorTemplates \\首页\\ EditorTemplates \\ ,并添加一个名为新视图 Jerk.cshtml 并分配方式模型。

All Html For Helpers provided by Asp.Net MVC use the model of the defined model for the page. What you can do is create an EditorTemplate. Create a subdirectory in your View folder called EditorTemplates such as in \Home\EditorTemplates\ and add a new view called Jerk.cshtml and assign your model that way.

然后就可以使用 Html.HiddenFor(F =&GT; f.Name)等等每个属性。这会给你,你期待您键入的访问。

Then you can use the Html.HiddenFor(f => f.Name) and so forth for each property. This will give you your typed access that you're expecting.

在您的主视图中,你会做到以下几点:

In your main view you would do the following:

@foreach(var jerk in Model) {
    @Html.EditorFor(m => jerk)
}

或者也可以称之为 EditorForModel 它会自动通过循环并查找EditorTemplate。

or you can call EditorForModel where it will automatically loop through and look for the EditorTemplate.

@Html.EditorForModel()

EditorFor 将寻找EditorTemplate您创建的第一个,然后它会从那里直到它找到一个覆盖或输出一个MVC认为你可能需要。

The EditorFor will look for the EditorTemplate you created first then it will go from there until it either finds an override or it will output one MVC thinks you might need.

这篇关于.NET MVC3 Html.HiddenFor foreach循环预期的集合里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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