一直试图关注一些在线示例,目的是为了使用视图模型而不再使用视图模型。
我有一个名为' Property'我创建了一个名为' PropertyIndexViewModel'的ViewModel,我的视图现在正在引用它。
我的控制器操作是:
// GET: Property ***TEST***
public async Task<ActionResult> Index1(int? id, PropertyIndexViewModel viewModel)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Property property = await db.Property.FindAsync(id);
if (property == null)
{
return HttpNotFound();
}
return View(viewModel);
}
我的观点不是抛出任何错误,但它也没有从模型中返回预期的数据?
答案 0 :(得分:2)
您必须初始化视图模型,使用Property模型数据填充它并将其返回。
// GET: Property ***TEST***
public async Task<ActionResult> Index1(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Property property = await db.Property.FindAsync(id);
if (property == null)
{
return HttpNotFound();
}
var viewModel = new PropertyIndexViewModel {
Prop1 = property.Prop1
// your stuff
};
return View(viewModel);
}
在您的视图中,您必须指定模型:
@model PropertyIndexViewModel