2013-08-29 8 views
8

ReadOnly属性はMVC 4ではないようです。編集可能(false)属性は、私が望むように機能しません。MVC 4カミソリデータAnnotations ReadOnly

類似した機能はありますか?

、その後どのように私はこのように動作します私自身の読み取り専用属性を作ることができない場合は、次の(仕事をしている)これに代えて

@Html.TextBoxFor(x=> x.aProperty) 

public class aModel 
{ 
    [ReadOnly(true)] or just [ReadOnly] 
    string aProperty {get; set;} 
} 

ので、私はこれを置くことができます。 (作業を行いますが、値が提出されていない)

@Html.TextBoxFor(x=> x.aProperty , new { @readonly="readonly"}) 

またはこの:

@Html.TextBoxFor(x=> x.aProperty , new { disabled="disabled"}) 

http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html

多分このような何か? https://stackoverflow.com/a/11702643/1339704

注:[編集可能(偽)]

答えて

9

あなたはReadOnly属性の存在のプロパティをチェックします。このようなカスタムヘルパーを作成することができます属性は単純である

public static MvcHtmlString MyTextBoxFor<TModel, TValue>(
    this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression) 
{ 
    var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
    // in .NET 4.5 you can use the new GetCustomAttribute<T>() method to check 
    // for a single instance of the attribute, so this could be slightly 
    // simplified to: 
    // var attr = metaData.ContainerType.GetProperty(metaData.PropertyName) 
    //     .GetCustomAttribute<ReadOnly>(); 
    // if (attr != null) 
    bool isReadOnly = metaData.ContainerType.GetProperty(metaData.PropertyName) 
           .GetCustomAttributes(typeof(ReadOnly), false) 
           .Any(); 

    if (isReadOnly) 
     return helper.TextBoxFor(expression, new { @readonly = "readonly" }); 
    else 
     return helper.TextBoxFor(expression); 
} 

を例:モデルの場合

public class ReadOnly : Attribute 
{ 

} 

public class TestModel 
{ 
    [ReadOnly] 
    public string PropX { get; set; } 
    public string PropY { get; set; } 
} 

私は以下のひげ剃りコードでこれが動作することを確認しました:

0123あなたは簡単に応じてヘルパーを変更することができます disabled代わり readonlyの必要がある場合は

<input id="PropX" name="PropX" readonly="readonly" type="text" value="Propx" /> 
<input id="PropY" name="PropY" type="text" value="PropY" /> 

:としてレンダリング

@Html.MyTextBoxFor(m => m.PropX) 
@Html.MyTextBoxFor(m => m.PropY) 

+0

新しいHtmlヘルパーを作成する代わりに、どのようなHtmlヘルパーでも動作させる方法はありますか? – Soenhay

+0

あなたが望む方法ですべてのヘルパーと動作させる方法はありません。そのため、ほとんどのヘルパーは、必要に応じて 'readonly'のようなものを指定できるオプションの' htmlAttributes'引数を持っています。私の上記の解決策は、与えた要件、つまり属性を使用して実装する方法です。もう一つの解決策は、 '@readonly =" readonly "'を指定するよりもきれいではないとは思うが、ヘルパーから出力されたHTML、すなわち '@ Html.TextBoxFor()に作用する拡張を作ることだろう。 Readonly() ' – asymptoticFault

+0

さて、htmlAttributesを単純な方法で使用することに決めましたが、私の質問に最も近い回答のように思われるので、私はあなたの答えを受け入れます。 – Soenhay

5

を動作しませんでした

あなたは独自のHTMLヘルパーメソッドを作成することができ

こちらをご覧ください: Creating Customer Html Helpers

を実際に - チェックアウトthis answer

public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
     this HtmlHelper<TModel> helper, 
     Expression<Func<TModel, TProperty>> expression) 
    { 
     return helper.TextBoxFor(expression, new { @readonly="readonly" }) 
    } 
+0

これは有望そうです...私はReadOnlyTextBoxForなどと呼ばれるものを作成することができます....私はデータの注釈が過度のものだと思います。 – Soenhay

+0

正確には、私の意見では、このようにして、あなたは正しい場所でそれをやっています。データアノテーションは、基本的にプレゼンテーション属性を定義する方法だとは思わない。 – KerSplosh

+0

データアノテーションは、プロパティを表示するための情報、つまり、「Display」および「DisplayFormat」データアノテーションを提供することができ、しばしば行うことができる。 – asymptoticFault