Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stored procedures ASP.Net MVC 5存储过程输出错误(Beginer)_Stored Procedures_Asp.net Mvc 5_Entity Framework 6 - Fatal编程技术网

Stored procedures ASP.Net MVC 5存储过程输出错误(Beginer)

Stored procedures ASP.Net MVC 5存储过程输出错误(Beginer),stored-procedures,asp.net-mvc-5,entity-framework-6,Stored Procedures,Asp.net Mvc 5,Entity Framework 6,存储过程 USE [College] GO ALTER PROCEDURE [dbo].[Usp_Employee] @Mode TINYINT =NULL AS BEGIN IF @Mode=1 BEGIN SELECT EmplId,EmplName,EmpRole,Salary from [dbo].[Employee] ORDER BY EmplName; END ELSE IF @Mode=2 BEGI

存储过程

USE [College]
GO

ALTER PROCEDURE [dbo].[Usp_Employee]
    @Mode TINYINT =NULL
AS
BEGIN
    IF @Mode=1
    BEGIN
        SELECT  EmplId,EmplName,EmpRole,Salary from [dbo].[Employee]
        ORDER BY EmplName;
    END
    ELSE IF @Mode=2
    BEGIN
        SELECT  EmplId,EmplName from [dbo].[Employee]
        ORDER BY EmplName;
    END
END
雇员(模范班)

namespace MVCQuestion.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类雇员
{
公共短模板ID{get;set;}
公共字符串名称{get;set;}
公共字符串{get;set;}
公共可空薪资{get;set;}
}
}
查看页面

@model IEnumerable<MVCQuestion.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Employee Name and Role </h2>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmplName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmpRole)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmplName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmpRole)
        </td>
    </tr>
}

</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
员工姓名和角色
@DisplayNameFor(model=>model.EmplName)
@DisplayNameFor(model=>model.EmpRole)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.EmplName)
@DisplayFor(modelItem=>item.EmpRole)
}
控制器

using MVCQuestion.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCQuestion.Controllers
{
    public class EmployeeController : Controller
    {
        CollegeEntities objDbContext = new CollegeEntities();
        // GET: Employee
        public ActionResult Index()
        {
            var Query = "exec [dbo].Usp_Employee @Mode=2";
            var Output = objDbContext.Database.SqlQuery<Employee>(Query).ToList<Employee>();
            return View(Output);
        }
    }
}
使用MVCQuestion.Models;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间MVCQuestion.Controllers
{
公共类EmployeeController:控制器
{
CollegeEntities objDbContext=新的CollegeEntities();
//获取:员工
公共行动结果索引()
{
var Query=“exec[dbo].Usp_Employee@Mode=2”;
var Output=objDbContext.Database.SqlQuery(Query.ToList();
返回视图(输出);
}
}
}
错误

数据读取器与指定的“CollegeModel.Employee”不兼容。“EmpRole”类型的成员在数据读取器中没有同名的对应列

我使用一个存储过程来获取Employee中的所有列,并且通过传递Mode变量仅获取2列。 在上面的代码中,我使用模式2从Employee表中获取两列,其中EmpRole和Salary被跳过。 好的,我的问题是,有没有办法只从存储过程中获取2个参数,并将其存储到新的Employee模型类列表中,而不生成额外的类 也就是说,在不初始化Employee模型类内的所有变量的情况下,获取Employee模型类列表对象中的数据的任何方法。
谢谢..

我认为您必须在控制器内填充视图模型。您的控制器在MVC方面看起来很不传统。用数据填充模型,然后在视图中返回模型,而不是输出。你能举个例子吗,
using MVCQuestion.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCQuestion.Controllers
{
    public class EmployeeController : Controller
    {
        CollegeEntities objDbContext = new CollegeEntities();
        // GET: Employee
        public ActionResult Index()
        {
            var Query = "exec [dbo].Usp_Employee @Mode=2";
            var Output = objDbContext.Database.SqlQuery<Employee>(Query).ToList<Employee>();
            return View(Output);
        }
    }
}