Asp.net mvc 控制器错误,无法在MVC中查看

Asp.net mvc 控制器错误,无法在MVC中查看,asp.net-mvc,model-view-controller,view,Asp.net Mvc,Model View Controller,View,鉴于此,我写道 if (user.UserName == DbData.UserName && DbData.Password.Equals(user.Password, tringComparison.CurrentCultureIgnoreCase)) { //redirect to main page //this is working fine } else { Model

鉴于此,我写道

    if (user.UserName == DbData.UserName && DbData.Password.Equals(user.Password, tringComparison.CurrentCultureIgnoreCase))
                {
                   //redirect to main page //this is working fine
}

    else {
      ModelState.AddModelError("Error", "Username/Password not matching");
   ViewBag.Message = ModelState["Error"].Errors[0].ErrorMessage;
  return View();
    }
查看包。留言

ModelState["Error"].Errors[0].ErrorMessage
}


但是viewbag中的错误不会出现在视图上,这是正常的,如果您在即时窗口中检查TempData[“error”],您将看到它为空。 你需要给观景袋一些东西才能在观景区得到一些东西

更新: 这会给你留言(
“用户名/密码不匹配”),您可以将其设置为ViewBag

@if(ViewBag.Message != null){
希望这能解决你的问题

有关您的信息: ModelStateDictionary.AddModelError方法(字符串,异常): 将指定的模型错误添加到与指定键关联的模型状态字典的错误集合中。 从

更新: 要在视图中显示消息,请在ViewBag.message前面使用@符号

ModelState["Error"].Errors[0].ErrorMessage

返回视图时,
ModelState
将被丢弃,因为您没有将模型传递到视图中--
TempData[“Error”]
是否有值?“用户名/密码不匹配”将删除此字符串there@Html.Raw(ViewBag.Error);这不会在视图中显示值。请删除以下内容:ViewBag.error=TempData[“error”];并替换为ViewBag.error=“纯文本”;然后让我们知道ViewBag是否仍然为空。正如我在上面的回答中所说,您没有在ViewBag中设置正确的值。如果视图中显示“纯文本”,则尝试提供modelState错误,如上所述。祝你好运如果我的答案对你有帮助,请把它标记为答案并投赞成票。thanksiam从中获取数据,但在查看时,Viewbag中的数据未呈现在视图中的这一行上放置断点:@if(Viewbag.Message!=null){然后在即时窗口中写入此内容:Viewbag.Message并按enter。告诉我显示的值是什么。