查看的模型
因为我是ASP.NET MVC的新手得到这个错误我想向用户显示个人资料"名称'o'在当前上下文中不存在"
查看
@model Test.Models.Customer
Name: @o.CST_Name
Email: @o.CST_Email
Gender: @o.CST_Gender
控制器
public ActionResult ViewProfileCustomer(string id)
{
var user = db.Customers.Single(u => u.CST_Username == id);
return View(user);
}
它显示此错误pic
@model Test.Models.Customer
Name: @Model.CST_Name
Email: @Model.CST_Email
Gender: @Model.CST_Gender
您始终可以访问通过@Model
当视图试图访问null对象上的属性时,显示错误,因此'no object'='no属性'。确保该用户对象在操作后调用后有一些数据,但是此代码无论如何都应显示视图内容。
控制器
public ActionResult ViewProfileCustomer(string id)
{
var user = db.Customer.FirstOrDefault(u => u.CST_Username == id);
return View(user);
}
查看@model test.models.customer
Name: @Html.DisplayFor(model => model.CST_Name)
Email: @Html.DisplayFor(model => model..CST_Email
Gender: @Html.DisplayFor(model => model..CST_Gender)