Home>

Thank you very much. ASP.NET mvc5 I am a beginner making a bulletin board.
Currently, when a button was clicked using the Ajax function, a function that sorts by category that matches the button name was implemented, but because a problem occurred that SQL statements were displayed instead of data, a question was posted . I want to realize an implementation that displays sorted data instead of SQL statement when the button is displayed.

Error message If i click the button marked

ALL, the SQL statement will be displayed. I want to display data instead of SQL statements.

Applicable source code

View

@using (Ajax.BeginForm ("Topics", new AjaxOptions ()
    {
        UpdateTargetId = "TopicList"
    }))
    {
        <input type = "submit" name = "All" value = "ALL" />
        <input type = "submit" name = "Turf" value = "turf" />
    }
    <div>
        <span></span>
    </div>

Controller

// GET: NewTopics
        // When opening the page, first display all items
        public async Task<ActionResult>Index ()
        {
            var Topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            return View (await Topics.ToListAsync ());
        }
        public IQueryable Topics (string All, string Turf)
        {
            var topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            if (All! = null)
            {
                // Processing when All is pressed
                topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            }
            if (Turf! = null)
            {
                // Processing when turf is pressed
                topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions) .Where (t =>t.CategoryId == 1);
            }

            return topics;
        }
Postscript trials

public IQueryable Topics (string All, string Turf) {} does not execute a query, so I tried to point out that an SQL statement appears in the return value. I tried 2 types as the last two lines, but I don't know why either of them gives an error. Thanks for your patience.

public async Task<ActionResult>Topics (string All, string Turf)
        {
            var topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            if (All! = null)
            {
                // Processing when All is pressed
                topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions);
            }
            if (Turf! = null)
            {
                // Processing when turf is pressed
                topics = db.Topics.Include (t =>t.AspNetUsers) .Include (t =>t.Categories) .Include (t =>t.Regions) .Where (t =>t.CategoryId == 1);
            }
            // Both lines from here will cause an error.
            var result = await Topics.ToListAsync ();// ToArrayAsync ()?
            return topics.ToListAsync;
        }
Referenced sites

Easy usage of Ajax.BeginForm
http://happyskiing.blog89.fc2.com/blog-entry-9.html

Supplemental information (FW/tool version etc.)

VS2017
Windows7
.Net Framework 4.6.1
MVC5

  • Answer # 1

    You don't have a partial view (.cshtml), right? I think that is the basis of all problems. I'll give you the information because it feels like you're getting lost.

    First, make sure AjaxHelper works. AjaxHelper should not work by default for MVC5 apps created with templates. See the following article for detailed instructions.

    AjaxHelper does not work with MVC5
    http://surferonwww.info/BlogEngine/post/2018/05/28/ajaxhelpers-do-not-work-on-mvc5-application-created-by-visual-studio-template.aspx

    And use partial view. The image of the above article was specified using Ajax.ActionLink is used to get the name of the customer from the following AdventureWorksLT Customers table and create an action link list in the view Index.cshtml. The customer details are acquired and displayed in a partial view (Details.cshtml).

    Upload the sample code that displays the image of the above article. If you look at this, you will understand what I pointed out that there is no partial view.

    Controller/Action method

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using AdventureWorksLT;
    using Mvc5App.Models;
    namespace Mvc5App.Controllers
    {
        public class CustomerController: Controller
        {
            public ActionResult Index ()
            {
                var db = new AdventureWorksLTEntities ();
                // Take (10) is long if you just get all records
                // Reason too
                var list = (from c in db.Customer
                            orderby c.CustomerID ascending
                            select c) .Take (10);
                return View (list);
            }
            public ActionResult Details (int id)
            {
                var db = new AdventureWorksLTEntities ();
                var details = (from c in db.Customer
                               where c.CustomerID == id
                               select c) .FirstOrDefault ();
                return PartialView (details);
            }
        }
    }

    View Index.cshtml

    @model IEnumerable<AdventureWorksLT.Customer>
    @ {
        ViewBag.Title = "Index";
    }
    <h2>Index</h2>
    <table>
        <tr>
            <td><ul>
                    @foreach (var item in Model)
                    {
                        <li>
                            @ Ajax.ActionLink (
                                item.Title + "" + item.FirstName + "" + item.LastName,
                                "Details",
                                new {id = item.CustomerID},
                                new AjaxOptions () {UpdateTargetId = "results"})
                        </li>
                    }
                </ul>
            </td>
            <td>
                <div id = "results"></div>
            </td>
        </tr>
    </table>
    @section Scripts {
        @ Scripts.Render ("~/bundles/jqueryval")
    }

    Partial view Details.cshtml

    @model AdventureWorksLT.Customer
    <fieldset>
        <legend>Customer Details</legend>
        @ Html.DisplayNameFor (model =>model.Title): @ Html.DisplayFor (model =>model.Title)
        <br />
        @ Html.DisplayNameFor (model =>model.FirstName): @ Html.DisplayFor (model =>model.FirstName)
        <br />
        @ Html.DisplayNameFor (model =>model.MiddleName): @ Html.DisplayFor (model =>model.MiddleName)
        <br />
        @ Html.DisplayNameFor (model =>model.LastName): @ Html.DisplayFor (model =>model.LastName)
        <br />
        @ Html.DisplayNameFor (model =>model.CompanyName): @ Html.DisplayFor (model =>model.CompanyName)
        <br />
        @ Html.DisplayNameFor (model =>model.SalesPerson): @ Html.DisplayFor (model =>model.SalesPerson)
        <br />
        @ Html.DisplayNameFor (model =>model.EmailAddress): @ Html.DisplayFor (model =>model.EmailAddress)
        <br />
        @ Html.DisplayNameFor (model =>model.Phone): @ Html.DisplayFor (model =>model.Phone)
    </fieldset>

  • Answer # 2

    If IQueryable is not executed, the query has not been executed yet (SQL is exposed).

  • Answer # 3

    var result = await Topics.ToListAsync ();// ToArrayAsync ()?
    return topics.ToListAsync;


    var result = await topics.ToArrayAsync ();
    return result;


    And.