Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编辑器,用于生成不正确的html_C#_Asp.net Mvc 3_Viewmodel_Data Annotations_Editorfor - Fatal编程技术网

C# 编辑器,用于生成不正确的html

C# 编辑器,用于生成不正确的html,c#,asp.net-mvc-3,viewmodel,data-annotations,editorfor,C#,Asp.net Mvc 3,Viewmodel,Data Annotations,Editorfor,我有一个MVCWeb项目,其中我尝试使用EditorFor扩展方法呈现复选框列表,但结果只是将ID显示为文本,而不是复选框列表 以下是视图中的代码: <div id="permissions" class="tab-body"> @Html.Label("Permissions :") @Html.EditorFor(x => Model.Permissions) <br /> <br /> </di

我有一个MVCWeb项目,其中我尝试使用EditorFor扩展方法呈现复选框列表,但结果只是将ID显示为文本,而不是复选框列表

以下是视图中的代码:

  <div id="permissions" class="tab-body">
     @Html.Label("Permissions :")
     @Html.EditorFor(x => Model.Permissions)
     <br />
     <br />
  </div>
}

最后,这是浏览器中的结果:

<div id="permissions" class="tab-body" style="display: block;">
<label for="Permissions_:">Permissions :</label>
192023242526272829
<br>
<br>
</div>

权限:
192023242526272829


你知道为什么html不能正确生成吗?缺少依赖项?依赖冲突?Web.Config配置不正确


非常感谢你的帮助。

也许你想自己做点什么

    public delegate object Property<T>(T property);

    public static HtmlString MultiSelectListFor<TModel, TKey, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Func<TProperty, TKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression,
        object htmlAttributes) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TProperty);

        //check for problems
        if (enumeratedItems == null) throw new ArgumentNullException("The list of items cannot be null");

        //build the select tag
        var returnText = string.Format("<select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        //build the options tags
        foreach (TProperty listItem in enumeratedItems)
        {
            var idValue = idExpression(listItem).ToStringOrEmpty();
            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (propertyValue.Contains(idValue))
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", HttpUtility.HtmlEncode(displayValue));
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }
公共委托对象属性(T属性);
公共静态HtmlString MultiSelectListFor(
这个HtmlHelper HtmlHelper,
表达式forExpression,
IEnumerable枚举数据项,
Func idExpression,
属性表达式,
财产所有权表达式,
对象htmlAttributes),其中TModel:class
{
//初始化值
var metaData=modelmetada.FromLambdaExpression(forExpression,htmlhelp.ViewData);
var propertyName=metaData.propertyName;
var propertyValue=htmlHelper.ViewData.Eval(propertyName.ToString空();
var enumeratedType=typeof(TProperty);
//检查问题
如果(enumeratedItems==null)抛出新ArgumentNullException(“项列表不能为null”);
//构建选择标记
var returnText=string.Format(“\n”;
//构建选项标签
foreach(enumeratedItems中的tpProperty listItem)
{
var idValue=idExpression(listItem).toString空();
var displayValue=displayExpression(listItem).ToString空();
var titleValue=titleExpression(listItem).toString空();
returnText+=string.Format(“{0}\n”,HttpUtility.HtmlEncode(displayValue));
}
//关闭选择标记
returnText+=“”;
返回新的HtmlString(返回文本);
}

也许你想自己做点什么

    public delegate object Property<T>(T property);

    public static HtmlString MultiSelectListFor<TModel, TKey, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, IEnumerable<TKey>>> forExpression,
        IEnumerable<TProperty> enumeratedItems,
        Func<TProperty, TKey> idExpression,
        Property<TProperty> displayExpression,
        Property<TProperty> titleExpression,
        object htmlAttributes) where TModel : class
    {
        //initialize values
        var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData);
        var propertyName = metaData.PropertyName;
        var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty();
        var enumeratedType = typeof(TProperty);

        //check for problems
        if (enumeratedItems == null) throw new ArgumentNullException("The list of items cannot be null");

        //build the select tag
        var returnText = string.Format("<select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName));
        if (htmlAttributes != null)
        {
            foreach (var kvp in htmlAttributes.GetType().GetProperties()
             .ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)))
            {
                returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key),
                 HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty()));
            }
        }
        returnText += ">\n";

        //build the options tags
        foreach (TProperty listItem in enumeratedItems)
        {
            var idValue = idExpression(listItem).ToStringOrEmpty();
            var displayValue = displayExpression(listItem).ToStringOrEmpty();
            var titleValue = titleExpression(listItem).ToStringOrEmpty();
            returnText += string.Format("<option value=\"{0}\" title=\"{1}\"",
                HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue));
            if (propertyValue.Contains(idValue))
            {
                returnText += " selected=\"selected\"";
            }
            returnText += string.Format(">{0}</option>\n", HttpUtility.HtmlEncode(displayValue));
        }

        //close the select tag
        returnText += "</select>";
        return new HtmlString(returnText);
    }
公共委托对象属性(T属性);
公共静态HtmlString MultiSelectListFor(
这个HtmlHelper HtmlHelper,
表达式forExpression,
IEnumerable枚举数据项,
Func idExpression,
属性表达式,
财产所有权表达式,
对象htmlAttributes),其中TModel:class
{
//初始化值
var metaData=modelmetada.FromLambdaExpression(forExpression,htmlhelp.ViewData);
var propertyName=metaData.propertyName;
var propertyValue=htmlHelper.ViewData.Eval(propertyName.ToString空();
var enumeratedType=typeof(TProperty);
//检查问题
如果(enumeratedItems==null)抛出新ArgumentNullException(“项列表不能为null”);
//构建选择标记
var returnText=string.Format(“\n”;
//构建选项标签
foreach(enumeratedItems中的tpProperty listItem)
{
var idValue=idExpression(listItem).toString空();
var displayValue=displayExpression(listItem).ToString空();
var titleValue=titleExpression(listItem).toString空();
returnText+=string.Format(“{0}\n”,HttpUtility.HtmlEncode(displayValue));
}
//关闭选择标记
returnText+=“”;
返回新的HtmlString(返回文本);
}

看起来您需要为“PermissionViewModel”类创建一个编辑器模板,因为现在,MVC似乎对如何为如此复杂的对象创建编辑器感到困惑

在提供视图的文件夹中,添加一个名为“EditorTemplates”的文件夹

然后在该文件夹中添加新的局部视图。代码应为:

@model IEnumberable<PermissionViewModel>
@foreach(var permission in Model)
@Html.EditorFor(x => x.Name)
@Html.EditorFor(x => x.Description)
@Html.EditorFor(x => x.IsDistributable)
@Html.EditorFor(x => x.IsGranted)
@model IEnumberable
@foreach(模型中的var权限)
@EditorFor(x=>x.Name)
@EditorFor(x=>x.Description)
@EditorFor(x=>x.IsDistributable)
@EditorFor(x=>x.isgrated)
您还需要为Name类创建一个编辑器模板

所以现在在你看来你可以打电话

<div id="permissions" class="tab-body">
 @Html.Label("Permissions :")
 @Html.EditorFor(x => Model.Permissions)
 <br />
 <br />
</div>

@Label(“权限:”)
@EditorFor(x=>Model.Permissions)


MVC将知道如何使用您刚刚为获得许可而创建的编辑器模板


了解编辑器模板的一个很好的资源是:

看起来您需要为“PermissionViewModel”类创建一个编辑器模板,因为现在,MVC似乎对如何为如此复杂的对象创建编辑器感到困惑

在提供视图的文件夹中,添加一个名为“EditorTemplates”的文件夹

然后在该文件夹中添加新的局部视图。代码应为:

@model IEnumberable<PermissionViewModel>
@foreach(var permission in Model)
@Html.EditorFor(x => x.Name)
@Html.EditorFor(x => x.Description)
@Html.EditorFor(x => x.IsDistributable)
@Html.EditorFor(x => x.IsGranted)
@model IEnumberable
@foreach(模型中的var权限)
@EditorFor(x=>x.Name)
@EditorFor(x=>x.Description)
@EditorFor(x=>x.IsDistributable)
@EditorFor(x=>x.isgrated)
您还需要为Name类创建一个编辑器模板

所以现在在你看来你可以打电话

<div id="permissions" class="tab-body">
 @Html.Label("Permissions :")
 @Html.EditorFor(x => Model.Permissions)
 <br />
 <br />
</div>

@Label(“权限:”)
@EditorFor(x=>Model.Permissions)


MVC将知道如何使用您刚刚为获得许可而创建的编辑器模板


学习编辑器模板的一个很好的资源是:

这似乎与一致。这似乎与一致。这将允许一个漂亮的显示,但将其解析回模型可能会有点棘手。@JeremyHolovcs如果所有内容都连接正确,模型绑定应该在发布后注意将它们放在一起。事实上,当您在int或string上使用EditorFor MVC使用默认的编辑器模板,定义自己的模板应该不会引起任何问题。@Brenton,您给了我一个很好的指示,说明了我的页面没有正确呈现的原因!在我的项目中搜索后,我发现我在“视图”文件夹下有一个EditorTemplates文件夹,该文件夹中包含许多