为什么我不能传入html属性EditorFor()?例如;
<%= Html.EditorFor(model => model.Control.PeriodType,
new { disabled = "disabled", readonly = "readonly" }) %>
Run Code Online (Sandbox Code Playgroud)
我不想使用元数据
更新:解决方案是从视图中调用它:
<%=Html.EditorFor( model => model.Control.PeriodEndDate, new {Modifiable=model.Control.PeriodEndDateModifiable})%>
Run Code Online (Sandbox Code Playgroud)
并ViewData["Modifiable"]在我的自定义EditorTemplates/String.ascx中使用,其中我有一些视图逻辑,确定是否将readonly和/或disabled属性添加到输入传入的匿名对象EditorFor()是一个被调用的参数additionalViewData,其属性被传递给编辑器模板ViewData采集.
我创建了一个默认的MVC 3项目(使用razor),以演示一个问题.
在登录页面上,有一行:
@Html.TextBoxFor(m => m.UserName)
Run Code Online (Sandbox Code Playgroud)
如果我改为:
@Html.TextBoxFor(m => m.UserName, new { title = "ABC" })
Run Code Online (Sandbox Code Playgroud)
然后它呈现为(带有title属性):
<input data-val="true" data-val-required="The User name field is required." id="UserName" name="UserName" title="ABC" type="text" value="" />
Run Code Online (Sandbox Code Playgroud)
但是,如果我使它成为EditorFor:
@Html.EditorFor(m => m.UserName, new { title = "ABC" })
Run Code Online (Sandbox Code Playgroud)
然后它被渲染(没有title属性):
<input class="text-box single-line" data-val="true" data-val-required="The User name field is required." id="UserName" name="UserName" type="text" value="" />
Run Code Online (Sandbox Code Playgroud)
总而言之,当我使用EditorFor时,title属性会丢失.
我知道TextBoxFor的第二个参数叫做htmlAttributes,而对于EditorFor来说它是additionalViewData,但是我已经看到了EditorFor可以渲染这个参数提供的属性的例子.
任何人都可以解释我做错了什么,以及如何在使用EditorFor时获得title属性?
我可以在View上设置EditorFor控件的宽度吗?
我设置了一些参数:
[Required, DisplayName("Payee Name"), StringLength(50)]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法设置渲染的文本框的宽度.
<table width="300" border="0" cellpadding="3" cellspacing="0">
<tr>
<td>
<%=Html.LabelFor(m => m.Name)%>
</td>
<td>
<%=Html.EditorFor(m => m.Name)%>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这可以以某种方式完成吗?
我试过了:
<%=Html.EditorFor(m => m.Name, new {width=50)%>
Run Code Online (Sandbox Code Playgroud)
但没有快乐......
我想从MVC改变'editor for'文本框的样式,特别是我想让文本框更大.
我尝试过几种方式添加CSS,但无济于事.
包含 :
<td class="myCss"><%=Html.EditorFor(x => x.NickName)%></td>
Run Code Online (Sandbox Code Playgroud)
和
<td class="myCss"><%=Html.EditorFor(x => x.NickName, new { @class = "myCss" })%></td>
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我试图使用强类型视图的值格式化ASP.Net MVC的TextBoxFor呈现的日期.日期可以为空,所以如果它为null我想看到一个空白值,否则我希望以MM/dd/yyyy格式看到它.
<%= Html.TextBoxFor(model => model.BirthDate, new { style = "width: 75px;" })%>
Run Code Online (Sandbox Code Playgroud)
谢谢,
Paul Speranza
我可以使用a轻松完成此操作,TextBoxFor但如何使用EditorFor?
我想使用DataAnnotation [DataType(DataType.EmailAddress)]但是没有这样做.
我不太明白DataType注释实际上是做什么的,因为它乍一看似乎根本没有做任何事情.
我最近遇到了ASP.NET MVC显示模板的问题.说这是我的模特:
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Model());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的看法:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<DisplayTemplateWoes.Models.Model>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%: Html.DisplayForModel() %>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我出于某种原因需要所有字符串的显示模板,我将创建一个String.ascx局部视图,如下所示:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%: Model %> (<%: Model.Length %>)
Run Code Online (Sandbox Code Playgroud)
这就是问题 - 在运行时抛出以下异常: "传递到字典中的模型项是'System.Int32'类型,但是这个字典需要一个'System.String'类型的模型项."
似乎String.ascx用于Model类的整数和字符串属性.我希望它只用于字符串属性 - 毕竟它被命名为String.ascx而不是Object.ascx或Int32.ascx.
这是设计的吗?如果是的话 - 它是否在某处记录?如果没有 …
<%= Html.EditorFor(product => product.Name) %>
Run Code Online (Sandbox Code Playgroud)
我需要生成的输出设置autocomplete ="off"属性.
我错过了什么?
编辑:我正在寻找一个EditorFor的扩展方法,它接受属性的键/值字典,所以我可以像这样调用它: <%= Html.EditorFor(product => product.Name, new { autocomplete = "off" } ) %>
这是为LabelFor完成的,但需要针对EditorFor进行调整
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) {
return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag …Run Code Online (Sandbox Code Playgroud) 我对如何在MVC中设置输入字段的宽度提出了一些很好的建议:
@Html.TextBoxFor(model => model.Status.RowKey, new { size = 200, disabled = "disabled", @readonly = "readonly" })
Run Code Online (Sandbox Code Playgroud)
现在我已经尝试了另一种类型的字段,但它不起作用:
@Html.EditorFor(model => model.Status.BrowserTitle, new { size = 80 })
Run Code Online (Sandbox Code Playgroud)
我也想将此设置为80个字符但是当我这样做时,源不会改变并且不显示80.
BEV