繁体   English   中英

视图模型绑定

[英]Viewmodel binding

为什么我不能像这样从我的视图模型中显示值?

  @Html.DisplayFor(modelItem => item.Mailings.ScheduledTime)

我收到以下错误:

无法从用法中推断出方法的类型参数

我的观点正在使用

@model List<App.Models.ScheduledMailingsViewmodel>

视图模型

public class MailingViewModel
{
    public string ScheduledTime { get; set; }
    public string Title { get; set; }
}

public class ScheduledMailingsViewmodel
{
    public List<MailingViewModel> Mailings { get; set; }
}

控制者

public ActionResult Index()
{
    ScheduledMailingsViewmodel vm = new ScheduledMailingsViewmodel();
    vm.Mailings = new List<MailingViewModel>();

    using (db)
    {
        db.Database.Connection.Open();
        var command = db.Database.Connection.CreateCommand();
        command.CommandText = "dbo.GetMailingSchedules";
        command.Parameters.Add(new SqlParameter("@param", "somevalue"));
        command.CommandType = System.Data.CommandType.StoredProcedure;
        var rdr = (command.ExecuteReader());

        while (rdr.Read())
        {
            vm.Mailings.Add(new MailingViewModel { ScheduledTime = rdr[0].ToString(), Title = rdr[1].ToString() });
        }
    }

    return View(vm);
}

您正在从控制器发送视图模型,但是使用viewmodel @model List<App.Models.ScheduledMailingsViewmodel>发送视图@model List<App.Models.ScheduledMailingsViewmodel> 只需使用@model App.Models.ScheduledMailingsViewmodel

这个-

@Html.DisplayFor(modelItem => item.Mailings.ScheduledTime)

应更改为-

@foreach (var item in Model.Mailings.ToList()){

 @Html.DisplayFor(modelItem => item.ScheduledTime)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM