Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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
Asp.net mvc 3 用于只读的MVC3编辑器_Asp.net Mvc 3_Readonly_Editorfor - Fatal编程技术网

Asp.net mvc 3 用于只读的MVC3编辑器

Asp.net mvc 3 用于只读的MVC3编辑器,asp.net-mvc-3,readonly,editorfor,Asp.net Mvc 3,Readonly,Editorfor,我想在编辑页面中使用EditorFor使其成为只读 我尝试将只读和禁用设置为: <div class="editor-field"> @Html.EditorFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" }) </div> @EditorFor(model=>model.userName,新的{disabled=“disab

我想在编辑页面中使用EditorFor使其成为只读

我尝试将只读和禁用设置为:

<div class="editor-field">
        @Html.EditorFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" })
    </div>

@EditorFor(model=>model.userName,新的{disabled=“disabled”,@readonly=“readonly”})
但是,它不起作用。如何禁用编辑此字段


谢谢。

EditorFor html帮助程序没有采用html属性的重载。在这种情况下,您需要使用更具体的内容,如TextBoxFor:

<div class="editor-field">
    @Html.TextBoxFor(model => model.userName, new 
        { disabled = "disabled", @readonly = "readonly" })
</div>
然后,在Views/Shared/EditorTemplates文件夹中,创建一个文件userName.cshtml。在该文件中,输入以下内容:

@model string
@Html.TextBoxFor(m => m, new { disabled = "disabled", @readonly = "readonly" })

对于那些想知道为什么要使用editofof或者不想编辑的人,我举个例子

我的模型里有这个

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0: dd/MM/yyyy}")]
    public DateTime issueDate { get; set; }
当您想要显示该格式时,它的唯一工作方式是使用EditorFor,但我有一个用于“输入”的jquery日期选择器,因此它必须是只读的,以避免用户写下错误的日期

为了让它按我想要的方式工作,我把它放在视图中

     @Html.EditorFor(m => m.issueDate, new{ @class="inp", @style="width:200px", @MaxLength = "200"})
这在我的就绪函数中

     $('#issueDate').prop('readOnly', true);
我希望这会对其他人有所帮助。
对不起,我的英语是

为一组特定的视图创建一个编辑器模板(由一个控制器绑定):

在本例中,我有一个日期模板,但您可以将其更改为您想要的任何内容

下面是Data.cshtml中的代码:

@model Nullable<DateTime>

@Html.TextBox("", @Model != null ? String.Format("{0:d}",     ((System.DateTime)Model).ToShortDateString()) : "", new { @class = "datefield", type =    "date", disabled = "disabled"  @readonly = "readonly" }) 
我是这样做的:

型号:

[ReadOnly(true)]
public string Email { get { return DbUser.Email; } }
@Html.TheEditorFor(x => x.Email)
namespace System.Web.Mvc
{
    public static class CustomExtensions
    {
        public static MvcHtmlString TheEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            return iEREditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        private static MvcHtmlString iEREditorForInternal<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();

            TagBuilder builder = new TagBuilder("div");
            builder.MergeAttributes(htmlAttributes);

            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);


            string labelHtml = labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsRequired)
                labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression, new { @class = "required" }).ToHtmlString();


            string editorHtml = Html.EditorExtensions.EditorFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsReadOnly)
                editorHtml = Html.DisplayExtensions.DisplayFor(htmlHelper, expression).ToHtmlString();


            string validationHtml = Html.ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();

            builder.InnerHtml = labelHtml + editorHtml + validationHtml;

            return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
        }
    }
}
查看:

[ReadOnly(true)]
public string Email { get { return DbUser.Email; } }
@Html.TheEditorFor(x => x.Email)
namespace System.Web.Mvc
{
    public static class CustomExtensions
    {
        public static MvcHtmlString TheEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            return iEREditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        private static MvcHtmlString iEREditorForInternal<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();

            TagBuilder builder = new TagBuilder("div");
            builder.MergeAttributes(htmlAttributes);

            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);


            string labelHtml = labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsRequired)
                labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression, new { @class = "required" }).ToHtmlString();


            string editorHtml = Html.EditorExtensions.EditorFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsReadOnly)
                editorHtml = Html.DisplayExtensions.DisplayFor(htmlHelper, expression).ToHtmlString();


            string validationHtml = Html.ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();

            builder.InnerHtml = labelHtml + editorHtml + validationHtml;

            return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
        }
    }
}
扩展名:

[ReadOnly(true)]
public string Email { get { return DbUser.Email; } }
@Html.TheEditorFor(x => x.Email)
namespace System.Web.Mvc
{
    public static class CustomExtensions
    {
        public static MvcHtmlString TheEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            return iEREditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        private static MvcHtmlString iEREditorForInternal<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();

            TagBuilder builder = new TagBuilder("div");
            builder.MergeAttributes(htmlAttributes);

            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);


            string labelHtml = labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsRequired)
                labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression, new { @class = "required" }).ToHtmlString();


            string editorHtml = Html.EditorExtensions.EditorFor(htmlHelper, expression).ToHtmlString();

            if (metadata.IsReadOnly)
                editorHtml = Html.DisplayExtensions.DisplayFor(htmlHelper, expression).ToHtmlString();


            string validationHtml = Html.ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();

            builder.InnerHtml = labelHtml + editorHtml + validationHtml;

            return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
        }
    }
}
namespace System.Web.Mvc
{
公共静态类自定义扩展
{
公共静态MvcHtmlString编辑器(此HtmlHelper HtmlHelper,表达式,对象htmlAttributes=null)
{
返回iEREditorForInternal(htmlHelper,expression,htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
私有静态MvcHtmlString iEREditorForInternal(此HtmlHelper HtmlHelper、表达式表达式、IDictionary htmlAttributes)
{
如果(htmlAttributes==null)htmlAttributes=new Dictionary();
标记生成器=新标记生成器(“div”);
builder.MergeAttributes(HtmlatAttributes);
var metadata=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
字符串labelHtml=labelHtml=Html.LabelExtensions.LabelFor(htmlHelper,expression.ToHtmlString();
if(metadata.IsRequired)
labelHtml=Html.LabelExtensions.LabelFor(htmlHelper,expression,new{@class=“required”}).ToHtmlString();
字符串editorHtml=Html.EditorExtensions.EditorFor(htmlhelp,expression).ToHtmlString();
if(metadata.IsReadOnly)
editorHtml=Html.DisplayExtensions.DisplayFor(htmlHelper,expression).ToHtmlString();
字符串validationHtml=Html.ValidationExtensions.ValidationMessageFor(htmlHelper,expression).ToHtmlString();
builder.InnerHtml=labelHtml+editorHtml+validationHtml;
返回新的MvcHtmlString(builder.ToString(TagRenderMode.Normal));
}
}
}

当然,我的编辑器正在做更多的事情,比如添加一个标签,根据需要向标签添加一个必需的类,如果属性是只读的,则添加一个DisplayFor,为添加一个
ValidateMessageFor
,最后将所有内容包装到一个
Div
中,该Div可以为其分配
Html属性
。。。我的
视图
非常干净。

我认为通过使用[Editable(false)]属性,这比其他视图更简单

例如:

 public class MyModel
    {
        [Editable(false)]
        public string userName { get; set; }
    }

@EditorFor(model=>model.userName)
使用jquery禁用

<script type="text/javascript">
   $(document).ready(function () {
      $('#userName').attr('disabled', true);
     });
</script>

$(文档).ready(函数(){
$('#userName').attr('disabled',true);
});

MVC4及以后版本支持此代码

@Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", @readonly = "readonly" } })

我知道问题是MVC 3,但那是2012年,以防万一:

从MVC 5.1开始,您现在可以将HTML属性传递给
EditorFor
,如下所示:

@Html.EditorFor(x => x.Name, new { htmlAttributes = new { @readonly = "", disabled = "" } })
尝试使用:

@Html.DisplayFor(model => model.userName) <br/>
@Html.HiddenFor(model => model.userName)
@Html.DisplayFor(model=>model.userName)
@Html.HiddenFor(model=>model.userName)
您可以这样做:

@Html.EditorFor(m => m.userName, new { htmlAttributes = new { disabled = true } })

我知道这是一个老职位。。但现在你可以这样做,以保持对齐和所有外观一致

 @Html.EditorFor(model => model.myField, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })

我使用
readonly
属性而不是
disabled
属性,因为当字段为readonly时,这仍然会提交值

注意:任何readonly属性的存在都将使字段
只读
,即使设置为false,因此,我将编辑器分为以下代码

 @if (disabled)
 {
     @Html.EditorFor(model => contact.EmailAddress, new { htmlAttributes = new { @class = "form-control", @readonly = "" } })
 }
 else
 {
     @Html.EditorFor(model => contact.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
 }

如果它是只读的,那么它就不再是编辑器。它可能应该通过显示一个编辑器只读来击败它的目标。为什么不使用DisplayFor呢?可能重复的原因是希望使用user EditorFor(但禁用)的原因是外观,这就是我的原因。DisplayFor创建一个文本输出,没有封闭框,并且必须调整CSS以正确地在表单上对齐(不难,只是一些填充顶部)。但是你可能想要一个所有字段都在框中的表单,只读值显示为灰色。我找不到允许指定参数的重载。还有没有我错过的扩展方法?无法使用Intellisense找到它,也无法在msdn文档中找到它:@JotaBe我想你是对的,但我想我以前见过这些。可能是在MVC4的预发行版中,也可能是我想象出来的。无论哪种方式,我都更新了答案。在较新版本的MVC(我相信是4+?)中,
additionalViewData
对象可以具有
htmlAttributes
属性,该属性
EditorFor
以及类似用于html属性的属性。因此它将变成:
@Html.EditorFor(model=>model.userName,new{htmlAttributes=new{disabled=“disabled”,@readonly=“readonly”})
。谢谢你,Seichi!这才是真正解决问题的办法!这是一个很好的答案。它描述了它的起源