1

特定の条件で無効にしたいテキストエリアがあります。この情報を ViewBag パラメーターとして送信したいのですが、方法がわかりません。

私のビューのテキストエリアは次のようになります

@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.DisableProgressDetail })

そしてコントローラーには次のようなものがあります:

if(conditions)
    ViewBag.DisableProgressDetail = "disabled=\"disabled\"";

ただし、html 出力は次のようになります。

<textarea DisableProgressDetail="disabled=&quot;disabled&quot;" class="followUpProgress" cols="20" id="ProgressDetail" name="ProgressDetail" rows="2">
</textarea>
4

3 に答える 3

3

あなたが欲しいのはこれです:

@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })

次に、コントローラーで次のようにします。

ViewBage.DisableProgressDetail = "disabled";
于 2012-06-22T20:32:09.833 に答える
1

指定されていない場合、属性はプロパティの名前から取得されます。そのため、ViewBag プロパティの名前が付けられた html 属性を取得しています。それを機能させる1つの方法は次のとおりです。

// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", ViewBag.disabled })
-------------------------------------------------------------
// in the controller
ViewBag.disabled = "disabled";

そのアプローチが気に入らない場合は、次のように無効ビットを設定するだけです。

// in the view:
@Html.TextAreaFor(f => f.ProgressDetail, new { @class = "followUpProgress", disabled=ViewBag.DisableProgressDetail })
-------------------------------------------------------------
// in the controller:
if(conditions)
    ViewBag.DisableProgressDetail = "disabled";
else
    ViewBag.DisableProgressDetail = "false";

// or more simply
ViewBag.DisableProgressDetail = (conditions) ? "disabled" : "false";
于 2012-06-22T20:47:36.537 に答える
1

うまくいきません。これを試すことができます:

//In the controller

if(Mycondition){ ViewBag.disabled = true;}
else { ViewBag.disabled = false;}

//In the view

@Html.TextBoxFor(model => model.MyProperty, ViewBag.disabled ? (object)new { @class = "MyClass", size = "20", maxlength = "20", disabled = "disabled" } : (object)new { @class = "MyClass", size = "20", maxlength = "20" })
于 2014-03-12T20:12:14.400 に答える