如何使用 Entity Framework 在 MVC 中显示来自存储过程的 return 数据
How show the return data from stored procedure in MVC using Entity Framework
我是 MVC 的新手,Entity Framework 如果这是一个简单的问题,请原谅。
我正在开发一个简单的项目 (MVC5-EF6),它根据名字和姓氏以及 return 客户信息查询客户 table。我正在使用 Entity Framework 调用存储过程以获取 return 数据。
我已经导入了数据库,一切正常。我唯一不确定的是,我应该如何使用视图显示 return 数据?
我试图将数据放入我创建的模型中,但是当我调试数据时,虽然我不确定它应该是什么 return 在这一行中:
return View(customerModel);
因为我收到此错误:
The model item passed into the dictionary is of type 'CustomerPortal_MVC.Models.CustomerModel', but this dictionary requires a model item of type 'CustomerPortal_MVC.Customer'.
这是调用存储过程的方法:
public ActionResult Details()
{
using (var context = new CustomerPortalEntities())
{
var customers = context.Search_Customer_By_Name("NONA", "WHITE", null);
CustomerModel customerModel = new CustomerModel();
foreach (var item in customers)
{
customerModel.Account_Number = item.Account_Number;
customerModel.First_Name = item.First_Name;
customerModel.Last_Name = item.Last_Name;
customerModel.Payment_in = item.Payment_in;
}
return View(customerModel);
}
}
这是模特:
public class CustomerModel
{
public int Customer_ID { get; set; }
public string Account_Number { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Payment_in { get; set; }
}
这是自动填充的视图代码。
@model CustomerPortal_MVC.Customer
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Customer</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Account_Number)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Number)
</dd>
<dt>
@Html.DisplayNameFor(model => model.First_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.First_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Middle_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Middle_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Last_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Last_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Password)
</dt>
<dd>
@Html.DisplayFor(model => model.Password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isRegistered)
</dt>
<dd>
@Html.DisplayFor(model => model.isRegistered)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isActivated)
</dt>
<dd>
@Html.DisplayFor(model => model.isActivated)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isActive)
</dt>
<dd>
@Html.DisplayFor(model => model.isActive)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isLocked)
</dt>
<dd>
@Html.DisplayFor(model => model.isLocked)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Account_Payment_Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Payment_Status)
</dd>
<dt>
@Html.DisplayNameFor(model => model.unsuccessful_login_count)
</dt>
<dd>
@Html.DisplayFor(model => model.unsuccessful_login_count)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Username)
</dt>
<dd>
@Html.DisplayFor(model => model.Username)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Last4SSN)
</dt>
<dd>
@Html.DisplayFor(model => model.Last4SSN)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Create_Date)
</dt>
<dd>
@Html.DisplayFor(model => model.Create_Date)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Modified_Date)
</dt>
<dd>
@Html.DisplayFor(model => model.Modified_Date)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Account_Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Status)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Salt)
</dt>
<dd>
@Html.DisplayFor(model => model.Salt)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Customer_ID }) |
@Html.ActionLink("Back to List", "Index")
解法:
我把视图改成这样:
@model CustomerPortal_MVC.Models.CustomerModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Customer</h4>
<hr />
<table>
<tr>
<td>
@Html.DisplayFor(model => model.First_Name)
@Html.DisplayFor(model => model.Account_Number)
</td>
</tr>
</table>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Customer_ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
问题是您的视图期望的类型与您返回的类型不同。
在您的视图顶部,您可能有一行如下所示:
@model CustomerPortal_MVC.Customer
应该是:
@model CustomerPortal_MVC.Models.CustomerModel
无论您传递给 View() 的什么类型,都应该与视图期望的类型相同。这是优于 ViewBag 的优势之一,因为您可以对模型进行类型检查。
我是 MVC 的新手,Entity Framework 如果这是一个简单的问题,请原谅。
我正在开发一个简单的项目 (MVC5-EF6),它根据名字和姓氏以及 return 客户信息查询客户 table。我正在使用 Entity Framework 调用存储过程以获取 return 数据。
我已经导入了数据库,一切正常。我唯一不确定的是,我应该如何使用视图显示 return 数据?
我试图将数据放入我创建的模型中,但是当我调试数据时,虽然我不确定它应该是什么 return 在这一行中:
return View(customerModel);
因为我收到此错误:
The model item passed into the dictionary is of type 'CustomerPortal_MVC.Models.CustomerModel', but this dictionary requires a model item of type 'CustomerPortal_MVC.Customer'.
这是调用存储过程的方法:
public ActionResult Details()
{
using (var context = new CustomerPortalEntities())
{
var customers = context.Search_Customer_By_Name("NONA", "WHITE", null);
CustomerModel customerModel = new CustomerModel();
foreach (var item in customers)
{
customerModel.Account_Number = item.Account_Number;
customerModel.First_Name = item.First_Name;
customerModel.Last_Name = item.Last_Name;
customerModel.Payment_in = item.Payment_in;
}
return View(customerModel);
}
}
这是模特:
public class CustomerModel
{
public int Customer_ID { get; set; }
public string Account_Number { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Payment_in { get; set; }
}
这是自动填充的视图代码。
@model CustomerPortal_MVC.Customer
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Customer</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Account_Number)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Number)
</dd>
<dt>
@Html.DisplayNameFor(model => model.First_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.First_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Middle_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Middle_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Last_Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Last_Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Password)
</dt>
<dd>
@Html.DisplayFor(model => model.Password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isRegistered)
</dt>
<dd>
@Html.DisplayFor(model => model.isRegistered)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isActivated)
</dt>
<dd>
@Html.DisplayFor(model => model.isActivated)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isActive)
</dt>
<dd>
@Html.DisplayFor(model => model.isActive)
</dd>
<dt>
@Html.DisplayNameFor(model => model.isLocked)
</dt>
<dd>
@Html.DisplayFor(model => model.isLocked)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Account_Payment_Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Payment_Status)
</dd>
<dt>
@Html.DisplayNameFor(model => model.unsuccessful_login_count)
</dt>
<dd>
@Html.DisplayFor(model => model.unsuccessful_login_count)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Username)
</dt>
<dd>
@Html.DisplayFor(model => model.Username)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Last4SSN)
</dt>
<dd>
@Html.DisplayFor(model => model.Last4SSN)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Create_Date)
</dt>
<dd>
@Html.DisplayFor(model => model.Create_Date)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Modified_Date)
</dt>
<dd>
@Html.DisplayFor(model => model.Modified_Date)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Account_Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Account_Status)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Salt)
</dt>
<dd>
@Html.DisplayFor(model => model.Salt)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Customer_ID }) |
@Html.ActionLink("Back to List", "Index")
解法:
我把视图改成这样:
@model CustomerPortal_MVC.Models.CustomerModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Customer</h4>
<hr />
<table>
<tr>
<td>
@Html.DisplayFor(model => model.First_Name)
@Html.DisplayFor(model => model.Account_Number)
</td>
</tr>
</table>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Customer_ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
问题是您的视图期望的类型与您返回的类型不同。
在您的视图顶部,您可能有一行如下所示:
@model CustomerPortal_MVC.Customer
应该是:
@model CustomerPortal_MVC.Models.CustomerModel
无论您传递给 View() 的什么类型,都应该与视图期望的类型相同。这是优于 ViewBag 的优势之一,因为您可以对模型进行类型检查。