我正在尝试使用Model
将对象的集合从控制器传递到视图。
是否可以重命名@foreach (var question in Model)
以便我的视图可读?
例如而不是使用@foreach (var question in questions)
有没有办法说// HomeController.cs
public class HomeController : Controller
{
public IActionResult Index()
{
var repo = new QuestionRepository();
var questions = repo.FindAll();
return View(questions);
}
}
// Index.cshtml
@model IEnumerable<MyDomain.Models.Entity.Question>
<div class="questions-wrap">
<div class="questions">
@foreach (var question in Model)
{
// Do stuff with the question
}
</div>
</div>
?
SON.parse: unexpected character at line 1 column 1 of the JSON data
答案 0 :(得分:1)
如果让您满意,可以将其重新分配给变量。 :)
@model IEnumerable<MyDomain.Models.Entity.Question>
@{
var questions = Model;
}
<div class="questions-wrap">
<div class="questions">
@foreach (var question in questions)
{
// Do stuff with the question
}
</div>
</div>