John St
John St

Reputation: 61

Display stored procedure select results in ASP.NET MVC

I have been trying to do this for more than 8 hours - please help. Mind the edit at the bottom.

Stored procedure

CREATE PROCEDURE SIX6 
    (@YEAR INT) 
AS
BEGIN
    SELECT * 
    FROM
        (SELECT 
             Song, Artist, Year_Of_order, semester, sales
         FROM 
             panta 
         WHERE 
             Year_Of_order = @YEAR) AS panta 
    PIVOT
        (AVG([sales]) 
              FOR [semester] IN([1], [2], [3], [4])
        ) AS PivotTable
    ORDER BY 
        Year_Of_order;
END

Panta table

Panta table

Stored procedure results (which is what I want to display in html):

Stored Procedure Results

Model (class)

namespace DB.Models
{
    public class Six
    { 
            [DisplayName("Year of Order")]
            [Required]
            public int v1 { get; set; }
    }
}

Controller

namespace DB.Controllers
{
    public class SixController : Controller
    {
        private ChinookEntities db = new ChinookEntities();

        // GET: Six
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index2(Six model)
        {
            int year = model.v1;
            System.Data.Entity.Core.Objects.ObjectResult<SIX6_Result> res = db.SIX6(year);

            //return Json(new { Data = db.SIX6(year) }, JsonRequestBehavior.AllowGet);

            return View(res);
        }
    }
}

Index (View)

@model DB.Models.Six
@{
    ViewBag.Title = "Six";
}

<h2>Six</h2>

@using (Html.BeginForm("Index2", "Six", FormMethod.Post))
{
    @Html.TextBoxFor(x => x.v1, new { @type = "number", @class = "form-control", min = "2009", max = "2025", required = "required" });
    <button type="submit">Go</button>
}

Index2 (View)

@model IEnumerable<DB.Models.SIX6_Result>
@{
    ViewBag.Title = "Index2";
}

<h2>Index2</h2>

When I go to /Six/Index and insert a number, 2010 for example, I get redirected to Index2 as I should, but this error occurs:

enter image description here

doing the commented return instead:

return Json(new { Data = db.SIX6(year) }, JsonRequestBehavior.AllowGet);

outputs all the information in Json format

Edit: The previous error is resolved, i am now facing this error: "The name 'model' does not exist in the current context" under Index 2, on the foreach clause

Edit2: Fixed the error by changing Index2 to the following:

@model IEnumerable<DB.Models.SIX6_Result>
@{
    ViewBag.Title = "Index2";
}

<h2>Index2</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Artist)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Song)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Year_Of_order)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.C1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.C2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.C3)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.C4)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Artist)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Song)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Year_Of_order)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.C1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.C2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.C3)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.C4)
            </td>
        </tr>
    }

</table>

Upvotes: 0

Views: 1500

Answers (1)

Backs
Backs

Reputation: 24923

Change type of model to:

@model IEnumerable<DB.Models.SIX6_Result1>

Problem is that SP returns it's own model type, not DB.Models.Six you use for table.

Upvotes: 1

Related Questions