136

に html 属性を渡せないのはなぜEditorFor()ですか? 例えば;

<%= Html.EditorFor(model => model.Control.PeriodType, 
    new { disabled = "disabled", readonly = "readonly" }) %>

メタデータを使用したくない

更新:解決策は、ビューからこれを呼び出すことでした:

 <%=Html.EditorFor( model => model.Control.PeriodEndDate, new {Modifiable=model.Control.PeriodEndDateModifiable})%>

ViewData["Modifiable"]読み取り専用および/または無効な属性を入力に追加するかどうかを決定するビューロジックがあるカスタム EditorTemplates/String.ascx で使用します。渡される匿名オブジェクトEditorFor()は呼び出されるパラメーターadditionalViewDataであり、そのプロパティはViewDataコレクション。

4

14 に答える 14

125

Update MVC 5.1 は、以下のアプローチを直接サポートするようになったため、組み込みエディターでも機能します。http://www.asp.net/mvc/overview/releases/mvc51-release-notes#new-features (それは、同じように考えているか、私の答えを読んだかのどちらかです:)

更新終了

独自のエディター テンプレートを使用する場合、または MVC 5.1 を使用する場合は、組み込みエディターに対して以下のアプローチを直接サポートするようになりました。

@Html.EditorFor(modelItem => item.YourProperty, 
  new { htmlAttributes = new { @class="verificationStatusSelect", style = "Width:50px"  } })

次に、テンプレートで (MVC 5.1 の単純な型には必要ありません)

@Html.TextBoxFor(m => m, ViewData["htmlAttributes"])
于 2013-05-23T05:37:20.710 に答える
99

EditorForメタデータで動作するため、html 属性を追加したい場合はいつでも実行できます。もう 1 つのオプションは、単純にカスタム テンプレートを作成して使用することTextBoxForです。

<%= Html.TextBoxFor(model => model.Control.PeriodType, 
    new { disabled = "disabled", @readonly = "readonly" }) %>    
于 2010-09-17T12:52:01.930 に答える
43

MVC 5.1 の時点で、次のことができるようになりました。

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

http://www.asp.net/mvc/overview/releases/mvc51-release-notes#new-features

于 2013-12-10T16:02:36.860 に答える
5

MVC 5.1 EditorForの html 属性のVB.Net コード構文は次のとおりです。

@Html.EditorFor(Function(x) x.myStringProp, New With {.htmlAttributes = New With {.class = "myCssClass", .maxlength="30"}}))
于 2015-05-29T14:57:34.830 に答える
3

使ってみませんか

@Html.DisplayFor(model => model.Control.PeriodType)
于 2010-12-29T22:48:08.387 に答える
2

EditorFor は引き続き使用できます。スタイル/いずれかのhtml属性をViewDataとして渡すだけです。

@Html.EditorFor(model => model.YourProperty, new { style = "Width:50px" })

EditorFor はテンプレートを使用してレンダリングするため、プロパティの既定のテンプレートをオーバーライドして、スタイル属性を ViewData として渡すだけです。

したがって、 EditorTemplate は次のようになります。

@inherits System.Web.Mvc.WebViewPage<object>

@Html.TextBoxFor(m => m, new { @class = "text ui-widget-content", style=ViewData["style"] })
于 2013-01-29T11:08:53.460 に答える
2

メタデータを使用したくない場合は、[UIHint("PeriodType")]属性を使用してプロパティを装飾するか、複雑なタイプの場合は何も装飾する必要はありません。次に、EditorFor は、EditorTemplates フォルダーで PeriodType.aspx または ascx ファイルを探し、代わりにそれを使用します。

于 2010-09-17T13:34:02.020 に答える
2

今日、null 可能な bool にバインドするチェックボックスについて同じ問題に取り組んできました。モデル (コードではなく) を変更できないため、これを処理するより良い方法を考え出す必要がありました。少し強引ですが、遭遇する可能性のあるケースの 99% で機能するはずです。入力タイプごとに有効な属性を手動で作成する必要があることは明らかですが、チェックボックスのすべてを取得したと思います。

私の Boolean.cshtml エディター テンプレートでは:

@model bool?

@{
    var attribs = new Dictionary<string, object>();
    var validAttribs = new string[] {"style", "class", "checked", "@class",
        "classname","id", "required", "value", "disabled", "readonly", 
        "accesskey", "lang", "tabindex", "title", "onblur", "onfocus", 
        "onclick", "onchange", "ondblclick", "onmousedown", "onmousemove", 
        "onmouseout", "onmouseover", "onmouseup", "onselect"};
    foreach (var item in ViewData) 
    {
        if (item.Key.ToLower().IndexOf("data_") == 0 || item.Key.ToLower().IndexOf("aria_") == 0) 
        {
            attribs.Add(item.Key.Replace('_', '-'), item.Value);
        } 
        else 
        {
            if (validAttribs.Contains(item.Key.ToLower()))
            {
                attribs.Add(item.Key, item.Value);
            }
        }
    }
}

@Html.CheckBox("", Model.GetValueOrDefault(), attribs)
于 2012-08-14T19:35:16.490 に答える
1

Views / Shared / EditorTemplates/MyTypeEditor.vbhtmlでタイプの独自のテンプレートを作成するだけです

@ModelType MyType

@ModelType MyType
@Code
    Dim name As String = ViewData("ControlId")
    If String.IsNullOrEmpty(name) Then
        name = "MyTypeEditor"
    End If
End Code

' Mark-up for MyType Editor
@Html.TextBox(name, Model, New With {.style = "width:65px;background-color:yellow"})

モデルプロパティを使用して、ビューからエディターを呼び出します。

@Html.EditorFor(Function(m) m.MyTypeProperty, "MyTypeEditor", New {.ControlId = "uniqueId"})

VB構文を許してください。それが私たちの転がり方です。

于 2012-11-08T01:48:37.207 に答える
1
Html.TextBoxFor(model => model.Control.PeriodType, 
    new { @class="text-box single-line"})

このように使用できます。と同じ出力でHtml.EditorFor、html属性を追加できます

于 2012-07-26T16:01:14.503 に答える
1

私の場合、追加の属性を受け取ることができる HTML5 数値入力エディター テンプレートを作成しようとしていました。独自の HTML ヘルパーを作成するのがより適切な方法ですが、.ascx テンプレートが既にあるので、次の方法を使用しました。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input id="<%= Regex.Replace(ViewData.TemplateInfo.GetFullHtmlFieldId(""), @"[\[\]]", "_") %>" name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" type="number" value="<%= ViewData.TemplateInfo.FormattedModelValue %>"
<% if (ViewData["attributes"] != null)
   {
       Dictionary<string, string> attributes = (Dictionary<string, string>)ViewData["attributes"];
       foreach (string attributeName in attributes.Keys){%>
        <%= String.Format(" {0}=\"{1}\"", attributeName, attributes[attributeName])%>
       <% }
   } %> />

この醜いビットは、数値型の入力を作成し、キー「属性」を持つ ViewData ディクショナリを探します。キーと値のペアを属性として追加するディクショナリを反復処理します。ID 属性の Regex は無関係であり、コレクションで使用されると、通常はアンダースコアとしてエスケープされるGetFullHtmlFieldId()角かっこを含む ID を返すため、そこにあります。[]

このテンプレートは、次のように呼び出されます。

Html.EditorFor(m => m.Quantity, "NumberField", new { attributes = new Dictionary<string, string>() { { "class", "txtQuantity" } } }

冗長ですが、動作します。ディクショナリを使用する代わりに、テンプレートでリフレクションを使用して、プロパティ名を属性名として使用できます。

于 2014-09-12T21:33:36.430 に答える