【问题标题】:How show the return data from stored procedure in MVC using Entity Framework如何使用实体框架在 MVC 中显示存储过程的返回数据
【发布时间】:2016-11-19 17:10:08
【问题描述】:

我是 MVC 和实体框架的新手,所以如果这是一个简单的问题,请原谅。

我正在开发基于名字和姓氏查询客户表并返回客户信息的简单项目(MVC5-EF6)。我正在使用实体框架调用存储过程来返回数据。

我已导入数据库,一切似乎都正常。我唯一不确定的是,我应该如何使用视图显示返回数据?

我试图将数据放入我创建的模型中,但是当我调试数据时,虽然我不确定它应该在这一行中返回什么:

 return View(customerModel);

当我收到此错误时:

传入字典的模型项的类型为“CustomerPortal_MVC.Models.CustomerModel”,但此字典需要类型为“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>

【问题讨论】:

  • 请格式化您的代码
  • @albertjan 我不确定代码是否格式化,第一部分是错误消息而不是代码。
  • 错误信息是不言自明的。你不明白哪一部分?您的视图需要不同的模型...
  • 你能显示你的视图代码吗?
  • @TusharGupta 我已将视图添加到代码中。你是对的,视图正在使用不同的更新,因为它只是在我添加 streo proc 时自动填充。

标签: c# asp.net-mvc asp.net-mvc-5 entity-framework-6


【解决方案1】:

问题是您的视图期望的类型与您返回的类型不同。

在您的视图顶部,您可能有一行如下所示:

@model CustomerPortal_MVC.Customer

应该是:

@model CustomerPortal_MVC.Models.CustomerModel

您传递给 View() 的任何内容都应该与视图期望的类型相同。这是与 ViewBag 相比的优势之一,因为您可以对模型进行类型检查。

【讨论】:

  • 谢谢你,我已经为我的问题添加了解决方案。
猜你喜欢
  • 2014-08-20
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多