在asp.net mvc中重命名模型

时间:2019-01-12 20:19:20

标签: asp.net-mvc model-view-controller asp.net-core asp.net-core-mvc

我正在尝试使用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

1 个答案:

答案 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>