Home>

ASP.NET mvc5 I am a beginner making a bulletin board.
Currently implementing a function to narrow down the title of the thread displayed when pressing the tab (defined as Topics in DataBase) by type

return View ()


Since only one return value is applied to, I wrote a question because I was overwhelmed by the problem that multiple Linq statements could not be passed to View even if they were written to the controller.

Error message

I think it's too easy, but if you write like this without any other help,
Controller

// GET: NewTopics
        public async Task<ActionResult>Index ()
        {
            var topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            var TurfTopics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions) .Where (t =>t.CategoryId = = 1);
            return View (await topics.ToListAsync (), await topics.ToListAsync ());
        }


An error like this returned.

argument 1: cannot be converted from 'System.Collections.Generic<(solution name) .Models.Topics>' to 'string'
Argument 2: cannot be converted from 'System.Collections.Generic<(solution name) .Models.Topics>' to 'string'
Applicable source code

Controller

// GET: NewTopics
        public async Task<ActionResult>Index ()
        {
            // All DB Topics topics
            var topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            var TurfTopics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions) .Where (t =>t.CategoryId = = 1);
            return View (await topics.ToListAsync ());
        }

View

<div><!-Tab switch button from here->
                            <input type = "radio" name = "tab_item" checked>
                            <label for = "all">ALL</label>
                            <input type = "radio" name = "tab_item">
                            <input type = "radio" name = "tab_item">
                            <label for = "otherwise">Other</label>
                            <div><!-Tab switch button->
                                <table border = "0"&​​gt;<!-From here, display all cases->
                                    <tr>
                                        <th>NO.</th>
                                        <th>Title</th>
                                        <th>Less number</th>
                                        <th>HN</th><th colspan = "1">Date of last comment</th>
                                        <th>Category</th>
                                        <th>Region</th>
                                        <th>Views</th>
                                    </tr><!-/. note->
                                    @foreach (var item in Model)
                                    {
                                    <tr>
                                        <td>@ Html.DisplayFor (modelItem =>item.Id)</td>
                                        <td>@ Html.ActionLink (item.Title, "Index", new {controller = "Responses", id = item.Id})</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.ResponseCount)</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.AspNetUsers.HandleName)</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.RecentResDate)</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.Categories.Name)</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.Regions.Name)</td>
                                        <td>@ Html.DisplayFor (modelItem =>item.ViewCount)</td>
                                    </tr>
                                    }
                                </table><!-Show all entries so far, regardless of category->

  

Tried

When I examined it myself, I thought that I should use a function called Tuple, but I couldn't understand how to use Tuple.    If Controller's return View () was successfully handled, I thought that a property with a more detailed name might be added to the Model of the foreach argument

@foreach (var item in Model.All)
                                    {
                                     (Omitted)
                                    }
                                    @foreach (var item in Model.turf)
                                    {
                                    (Omitted)
                                    }

        

Supplemental information (FW/tool version, etc.)

VS2017 Windows7 .Net Framework 4.6.1 MVC5

  • Answer # 1

    Define a new model that can hold multiple models

    IEnumerable<Models.Topics>

    If it is too much trouble, put it in ViewBag.
    If the content is the same as ALL, you can also filter by script.

Related articles