数据读取器与MVC4中指定的模型不兼容

时间:2015-10-12 11:35:20

标签: asp.net-mvc-4

我正在开发我的MVC应用程序。我正在使用存储过程在数据库中插入值。在MVC中添加代码时,我遇到了错误。我将在这里分享我的代码。

等待列表

[WaitingListId] [int] IDENTITY(1,1) NOT NULL,
Primary Key - [CustomerId] [int] NULL,
Foreign key - [BusinessCategoryId] [int] NULL,
Foreign key - [BusinessId] [int] NULL,
Foreign key - [LocationId] [int] NULL,
Foreign key - [EmployeeId] [int] NULL,
[WaitingOrder] [int] NULL,
[Status] [nvarchar](50) NULL

存储过程

Alter procedure sp_waitinglist
@businessid int, 
@customerid int, 
@busictgryid int, 
@locid int, 
@empid int,
@sts nvarchar(50) 

As
declare @MaxVal as int
Begin

 If EXISTS(select * from tbl_WaitingList where BusinessId = @businessid )
 Begin

    SET @MaxVal = (SELECT MAX(WaitingOrder) +1 from tbl_WaitingList where BusinessId = @businessid )    
      insert into tbl_WaitingList(CustomerId, BusinessCategoryId,BusinessId,LocationId, EmployeeId, WaitingOrder, Status) values(@customerid, @busictgryid, @businessid,@locid,@empid,@MaxVal, 'yy')

    End

    else

     Begin
       SET @MaxVal= 1
     insert into tbl_WaitingList(CustomerId, BusinessCategoryId,BusinessId,LocationId, EmployeeId, WaitingOrder, Status) values(@customerid, @busictgryid, @businessid,@locid,@empid,@MaxVal, 'yy')
    End
End

MVC控制器代码

public ActionResult WaitingList()
        {
            SYTEntities ca = new SYTEntities();
            ViewBag.CategoryId = new SelectList(ca.BusinessCategories.ToList(), "CategoryId", "CategoryName");
            ViewBag.Service = new SelectList(ca.tblBusinessCategories.ToList(), "BusinessID", "BusinessName");
            return View();
        }
        [HttpPost]
        public ActionResult WaitingList(string Business, string Service, string Location, string Employee)
        {
            ViewBag.CategoryId = new SelectList(db.BusinessCategories.ToList(), "CategoryId", "CategoryName");
            ViewBag.Service = new SelectList(db.tblBusinessCategories.ToList(), "BusinessID", "BusinessName");

            var param = new SqlParameter[6];
            param[0] = new SqlParameter { ParameterName = "@businessid", Value = Service };
            param[1] = new SqlParameter { ParameterName = "@customerid", Value = (int)Session["UserID"] };
            param[2] = new SqlParameter { ParameterName = "@busictgryid", Value = Business };
            param[3] = new SqlParameter { ParameterName = "@locid", Value = Location };
            param[4] = new SqlParameter { ParameterName = "@empid", Value = Employee };
           param[5] = new SqlParameter { ParameterName = "@sts", Value =  "gg" };
            List<tbl_WaitingList> waiting = new List<tbl_WaitingList>();
            using (SYTEntities context = new SYTEntities())
            {
                waiting = context.Database.SqlQuery<tbl_WaitingList>("exec sp_waitinglist @businessid,@customerid,@busictgryid,@locid,@empid, @sts", param).ToList();


            }

            db.SaveChanges();
            return View();

        }

查看页面

@using (Html.BeginForm("WaitingList", "Appt", FormMethod.Post))
{

 <div>
           @Html.DropDownList("Business", ViewBag.CategoryID as SelectList ,"Select the Business Category", new { id = "Business" })
       </div>
       <div>
           <label>Select the Service Type</label>
           @*<select id="Buname" name="buname" style="width:150px"></select>*@
           @Html.DropDownList("Service",  ViewBag.Service as SelectList,"Select", new { id = "Service", name ="buname"})
       </div>
       <div>
           <label> Select the Location</label>
           <select id="Location" name="location" style="width:150px"></select>
       </div>
       <div>
           <label> Select the Location</label>
           <select id="Employee" name="employee" style="width:150px"></select>
       </div>
  <div>
       <input type="submit" name="btn" value="Add me into waiting list"/>

   </div>
}

错误是

The data reader is incompatible with the specified 'SYTModel.tbl_WaitingList'. A member of the type, 'WaitingListId', does not have a corresponding column in the data reader with the same name.

我的错误页面是

enter image description here

我收到这样的错误。任何人都可以帮我解决这个问题。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

您应该按以下方式提供SqlParameter实例:

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>

namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;

enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };

std::map<TYPEX, std::string> author2name;
struct Emp {
    std::string name;
    TYPEX author;

    bool isRoby() const { return name == "roby"; };
};

BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
// BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost

int main() {
    using it = boost::spirit::ostream_iterator;

    karma::rule<it, std::string()> quote;
    karma::rule<it, TYPEX()> author;
    karma::rule<it, Emp()> emp;

    {
        using namespace karma;
        quote %= '"' << string << '"';
        author = quote[_1 = phx::ref(author2name)[_val]];

        emp    %= delimit('\t')[ quote << author << bool_ [ _1 = phx::bind(&Emp::isRoby, _val) ] ];
    }

    Emp x{ "one", AUTHOR2 };
    author2name[AUTHOR2] = "TWO!";
    std::cout << karma::format(emp, x);
}