【问题标题】:Display stored procedure select results in ASP.NET MVC在 ASP.NET MVC 中显示存储过程选择结果
【发布时间】:2020-04-18 16:35:04
【问题描述】:

我已经尝试执行此操作超过 8 小时 - 请帮助。 请注意底部的编辑。

存储过程

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

潘塔桌

存储过程结果(这是我想在html中显示的):

模型(类)

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

控制器

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);
        }
    }
}

索引(视图)

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

索引2(视图)

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

<h2>Index2</h2>

当我转到 /Six/Index 并插入一个数字(例如 2010)时,我会按我应该的方式重定向到 Index2,但是会发生此错误:

改为执行注释返回:

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

以Json格式输出所有信息

编辑: 之前的错误已解决,我现在面临这个错误: 在索引 2 下的 foreach 子句中“当前上下文中不存在名称‘模型’”

Edit2:通过将 Index2 更改为以下内容来修复错误:

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

【问题讨论】:

  • 之前的错误已经解决,新的错误是:索引2下foreach子句上的“名称'model'不存在于当前上下文中”

标签: c# html sql asp.net-mvc stored-procedures


【解决方案1】:

将模型类型更改为:

@model IEnumerable<DB.Models.SIX6_Result1>

问题是 SP 返回它自己的模型类型,而不是您用于表的 DB.Models.Six

【讨论】:

  • “名称‘模型’在当前上下文中不存在”在索引 2 下,在 foreach 子句中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 2014-05-23
相关资源
最近更新 更多