我有一个View绑定到包含Products和Contacts的ViewModel。我显示 视图的一部分中的产品和工作原理。我需要公开数据 联系人的定义,所以我可以 输入并发布到联系人模型。
首先在这一行失败 @ Html.LabelFor(model => model.ContactFName)
错误讯息:
'ProductPageViewModel'不包含'ContactFName'的定义
PRODUCTPAGEVIEWMODEL.CS
public class ProductPageViewModel
{
public IEnumerable<Products> Products { get; set; }
public IEnumerable<Contact> Contacts { get; set; }
public string VmMessage { get; set; }
}
产品控制器,指数行动
public ActionResult Index(string product)
{
if (String.IsNullOrEmpty(product))
{
product = "homepage";
}
var products = db.DbProducts.Where(m => m.Category == product);
var contacts = db.DbContacts;
var model = new ViewModel.ProductPageViewModel
{
Contacts = contacts,
Products = products,
};
return View(model);
}
产品型号
public class Products
{
[Key]
public int WpId { get; set; }
public string Category { get; set; }
public int CategoryId { get; set; }
public string ProdDescShort { get; set; }
public string ProdDescLong { get; set; }
public string ProductSmall { get; set; }
public string ProductMedium { get; set; }
public string ProductLarge { get; set; }
public decimal? Price1 { get; set; }
}
联系模式
public class Contact
{
public int? ContactId { get; set; }
public string ContactFName { get; set; }
public string ContactLName { get; set; }
public string ContactEmail { get; set; }
public string ContactComments { get; set; }
public string ContactBirthday { get; set; }
public string ContactSource { get; set; }
}
INDEX VIEW
@model JaniesWebLive.ViewModel.ProductPageViewModel
@using (Html.BeginForm("AddItem", "Carts"))
{
foreach (var item in Model.Products)
{
<p><img class="product-image" src=@Html.DisplayFor(modelItem => item.ProductMedium) /></p>
<h3>@Html.DisplayFor(modelItem => item.WpProductId)</h3>
<h5>@Html.DisplayFor(modelItem => item.Price1)</h5>
<h6>@Html.DisplayFor(modelItem => item.ProdDescShort)</h6>
<p class="text-center">@Html.DisplayFor(modelItem => item.ProdDescLong)</p>
<p>
<button class="text-right btn btn-link js-delete">ADD TO CART</button>
</p>
}
}
@using (Html.BeginForm("SubmitContact", "Products"))
{
<div class="form-group">
@Html.LabelFor(model => model.ContactFName)
@Html.TextBoxFor(model => model.ContactFName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ContactLName)
@Html.TextBoxFor(model => model.ContactLName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ContactEmail)
@Html.TextBoxFor(model => model.ContactEmail, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ContactComments)
@Html.TextAreaFor(model => model.ContactComments, new { @class = "form-control", rows = 3 })
</div>
@Html.Hidden("ContactSource", "DealsSignup")
<button type="submit" class="btn btn-primary">Submit</button>
}
答案 0 :(得分:0)
产品控制器,指数行动 尝试: var contacts = db.DbContacts **。Tolist(); **
甚至在viewModel初始化中你已经可以放了:
Contacts = db.DbContacts **。Tolist(); **
你传递的是具体的产品,但有一组联系方式。
nearbySearch