【发布时间】:2012-03-29 14:36:21
【问题描述】:
我花了大约 3 个小时才确定为什么会发生以下(解释的)错误;最后我确定这是一个 mvc 错误。我想问问你的意见。
我创建了一个编辑器模板并将其放在 ~/Shared/EditorTemplates 文件夹下。而且我小心地使用文件名与类型名相等(因为我不想将第二个参数与“@Html.EditorFor”帮助方法一起使用)。结果编辑器模板工作正常:除了回发;我肯定无法从模型中获取值,因为模型始终为空。我尝试更改输入的 id 和名称并仔细选择并回复:它工作正常。
所以;问题是:当我使用 editorTemplate 时,它会给出“Question.[0].QuestionContext”作为名称,并给出“Question__0_QuestionContext”作为输入和选择的 id;但是我期待“Question[0].QuestionContext”作为名称,“Question_0__QuestionContext”作为 id。
然后我意识到我在 EditorTemplate 中使用了@foreach。然后我切换回@for,但没有任何改变。您可以在下面找到模板:
@using DenemeMvc3Application3.Helpers;
@model DenemeMvc3Application3.Controllers.LocalizedNameViewModels
<table>
<thead>
<tr>
<td>@Html.LabelFor(modelItem => modelItem.ElementAtOrDefault(0).LanguageDropDownList)</td>
<td>@Html.LabelFor(modelItem => modelItem.ElementAtOrDefault(0).Value)</td>
</tr>
</thead>
<tbody>
@for (int index = 0; index < Model.Count; index++)
{
@Html.EditorFor(modelItem => modelItem[index])
}
</tbody>
</table>
所以我通过在编辑器模板之外和视图内部声明 foreach 循环并将编辑器模板仅用于单个项目,从而将我的模板更改为更简单的模板。它奏效了。因此,我对模板如何使用反射器在 mvc3 上工作进行了小调查;我发现了我认为的错误:
问题是由 System.Web.Mvc.Html.SelectExtensions 类的私有静态 MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, string optionLabel,字符串名称、IEnumerable selectList、bool allowMultiple、IDictionary htmlAttributes) 方法。 因此,每当我们在 EditorTemplate 中的 foreach 或 for 循环中使用 @Html.DropDownListFor(/blah blah/) 时,都会出现错误。
我还想向您展示下面的错误代码以澄清:
// TemplateInfo method
public string GetFullHtmlFieldName(string partialFieldName)
{
/*Here's the extra dot: causing the error.*/
return (this.HtmlFieldPrefix + "." + (partialFieldName ?? string.Empty)).Trim(new char[] { '.' });
}
// SelectExtensions method
private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool allowMultiple, IDictionary<string, object> htmlAttributes)
{
/* blah blah */
string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
/* blah blah */
}
// SelectExtensions method -> @Html.DropDownListFor
private static MvcHtmlString DropDownListHelper(HtmlHelper htmlHelper, string expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.SelectInternal(optionLabel, expression, selectList, false, htmlAttributes);
那么,你的想法是什么?
【问题讨论】:
标签: c# asp.net .net asp.net-mvc-3