Ask Your Question
4

In ASP.NET Core 6 MVC, can a List<string> be transmitted from the controller to the view as a component of the model?

asked 2023-05-21 21:39:10 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-21 21:53:02 +0000

david gravatar image

Yes, a List<string> can be transmitted from the controller to the view as a component of the model in ASP.NET Core 6 MVC. </string>

Here is an example of a controller action that creates a List<string> and includes it in the view's model:

public IActionResult Index() {  List<string> list = new List<string>{"foo", "bar", "baz"};  return View(list); } 

And here is an example of how to render the list in the view:

@model List<string> <ul>
    @foreach(var item in Model)
    {
        <li>@item</li>
    }
</ul> 

The resulting HTML output would be:

<ul> <li>foo</li> <li>bar</li> <li>baz</li> </ul> 
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-21 21:39:10 +0000

Seen: 7 times

Last updated: May 21 '23