[英]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.