software_writer
software_writer

Reputation: 4468

Rename the Model in asp.net mvc

I am trying to pass a collection of objects from a controller to the view, using a Model.

Is it possible to rename the Model so that my view is readable?

e.g. instead of using @foreach (var question in Model),

is there a way to say @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>

Upvotes: 0

Views: 1041

Answers (1)

Yom T.
Yom T.

Reputation: 9190

You could reassign it to a variable if that makes you happy. :)

@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>

Upvotes: 2

Related Questions