视图模型不返回任何数据

Viewmodel returning no data

一直在尝试遵循一些在线示例,目的是从使用视图模型转向使用视图模型。

我有一个名为 '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);
}

我的视图没有抛出任何错误,但它也没有从模型返回预期的数据?

您必须初始化视图模型,用 属性 模型数据和 return 填充它。

// 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