ASP.NET MVC razor html出错

ASP.NET MVC razor html出错

下面的Html.TextBox razor代码会出错

@using (Html.BeginForm())
        {
        
新用户注册 姓名:@Html.TextBox("name", ViewData["name"]) @*密码:@Html.TextBox("name", ViewBag.pwd)*@ 密码:@Html.TextBox("name", TempData["pwd"])
}

Action代码

public ActionResult Demo2Index()
        {
            ViewData["name"] = Request["name"];
            ViewBag.pwd = 123;
            TempData["pwd"] = Request["pwd"];
            return View();
        }
 

ViewBag.pwd是dynamic动态类型,Html.TextBox的重载第二个参数没有dynamic,所以报错了。改下面

@Html.TextBox("passwd",ViewBag.pwd as int?)

注意给ViewBag赋值的类型,要不as转换无法转换为null值,其他2个同理转为string或者其他类型,不能直接使用dynamic.

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632