将viewbag值与字符串

时间:2018-01-17 08:36:43

标签: javascript c# asp.net-mvc viewbag

我为控制器中的ViewBag.Form赋值

ViewBag.Form = "In";

现在,我想在javascript中将值与字符串进行比较。所以我这样做

 var form = "@ViewBag.Form";
if (form == "In")
{
//myCode
}
else
{
//myCode
}

但它总是进入else循环,这意味着形式和ViewBag.Form在实际匹配时不匹配

1 个答案:

答案 0 :(得分:0)

请在单引号中使用JavaScript中的viewbag

像这样的'@ ViewBag.Form'

<input type="hidden" id="FormID" value="@ViewBag.Form" />

var form = document.getElementById('FormID'); //javascript
var form = $("#FormID").val(); //jQuery
    if (form == "In")
    {
    //myCode
    }
    else
    {
    //myCode
    }

或 将ViewBag.Form值分配给隐藏字段

exec