Alma Alma - 17 days ago 5
C# Question

How show the return data from stored procedure in MVC using Entity Framework

I am new to MVC and Entity Framework so excuse me if it is simple question.

I am developing simple project(MVC5-EF6) that query the customer table based on First-name and last-name and return the customer info. I am using Entity Framework to call a stored procedure to return data.

I have imported the database and everything seems to be working. The only thing I am not sure about, is how should I show the return data using a view?

I tried to put the data in a Model that I had created, but when I debug the data is there though I'm not sure what it should return in this line:

return View(customerModel);


as I am getting this error:


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'.


This is the method that calls the stored procedure:

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


and this is model:

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


and this is the code of the View, that is Auto populated.

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




Solution:

I have changed the View like this:

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

Answer

The problem is your view is expecting a different type than what you're returning.

At the top of your view, you probably have a line that looks like:

@model CustomerPortal_MVC.Customer

It should be:

@model CustomerPortal_MVC.Models.CustomerModel

Whatever you pass to View() should be the same type that the view expects. This is one of the advantages over the ViewBag, in that you get type checking of your models.