2

我有一个文本区域,需要在某些条件下更改输入类型。我想将此信息作为 ViewBag 参数发送,但我不知道该怎么做。我虽然在控制器中这样做:

if(inputtext == 1)
  ViewBag.TextBox = @Html.TextBoxFor(m => m.Name, new { @class =\"inputtext\"})
if(inputtext == 2)
  ViewBag.TextBox = @Html.TextAreaFor(m => m.Name, new { @class =\"inputtext2\"})

我的观点是这样的:

@Html.Raw(ViewBag.TextBox)

但不要工作。关于如何做到这一点的任何想法?

4

1 回答 1

0

尝试这个:

在您的控制器中:

if(A){ ViewBag.NameControlType = "textbox" }
if(B){ ViewBag.NameControlType = "textarea" }

在你的cshtml中:

@if(null != (ViewBag.NameControlType as string))
{
    if(ViewBag.NameControlType == "textbox")
    {
        Html.TextBoxFor(m => m.Name, new { @class ="inputtext"})
    }
    if(ViewBag.NameControlType == "textarea")
    {
        Html.TextAreaFor(m => m.Name, new { @class = "inputtext2")
    }
}
于 2012-11-05T06:06:25.083 回答