2012-11-05 29 views
2

我有一個textarea,我需要在某些條件下更改輸入類型。我想以ViewBag參數的形式發送這些信息,但我無法弄清楚如何去做。不過,我覺得這在控制器:如何使用ViewBag在mvc中添加Html Helper

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) 

但不起作用。任何想法如何做到這一點?在您的控制器

:在您的CSHTML

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

回答

0

試試這個

@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") 
    } 
} 
相關問題