0
我是mvc的新手。我试图在我的“视图”中使用像if else一样的条件,但它在“Model.sayHello”中引发对象引用错误。模型在mvc aspx视图中为null
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<myMVC.Models.myMVCMaster>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<div>
<% using (Html.BeginForm("Index", "myMaster", FormMethod.Post, new { id = "SubmitForm" }))
{ %>
<%: Html.ValidationSummary(true)%>
<% if(Model.sayHello) {
<div>Hello world!</div>
<% }
} %>
</body>
</html>
控制器:
public class myMasterController : Controller
{
[HttpGet]
public ActionResult Index(myMVCMaster model)
{
model.InitializePage();
return View(model);
}
[HttpPost]
public ActionResult Index(myMVCMaster model, string Command)
{
}
}
型号:
public class myMVCMaster
{
private bool _sayHello = false;
[System.ComponentModel.DefaultValue(false)]
public bool sayHello{ get { return _sayHello; } set { _sayHello = value; } }
public void InitializePage()
{
}
}
为什么我的模型返回为空?
有你甚至配置项目中使用剃刀?视图中的其他代码('<%... %>')使用的是ASPX –
您已经在这里结合了两种语法 –
我认为您的视图不使用使用旧的aspx视图引擎的剃须刀视图引擎..所以请尝试使用<% %>而不是@。 –