通过ViewModels实现
建立一个新的文件夹 Viewmodel——>添加→类
类中添加
public class HomeViewModel
{
public IList<Noodle> Noodles { get; set; }
public IList<Feedback> Feedbacks { get; set; }
}
在controller中New一个HomeViewModel
public IActionResult Index()
{
//var noodles = _noodleRepository.GetAllNoodles();
var viewModle = new HomeViewModel()
{
Feedbacks = _feedbackRepository.GetAllFeedbacks().ToList(),
Noodles = _noodleRepository.GetAllNoodles().ToList()
};
return View(viewModle);
}
Views中可同时调用两个models
@model LanzhouBeefNoodles.ViewModels.HomeViewModel
@{
ViewData[“Title”] = “Index”;
}
foreach (var noodle in Model.Noodles)
{
@noodle.name
}@foreach (var feedback in Model.Feedbacks)
{
@feedback.name
}