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

asp.net-mvc asp.net-mvc-5 c# entity-framework-6

Question

Please excuse me if this is an easy question because I am new to MVC and Entity Framework.

I'm working on a straightforward project (MVC5-EF6) that queries the customer table based on the first and last names of the customer and returns that information. I'm calling a stored procedure with Entity Framework to get data back.

The database has been imported, and everything appears to be functioning. The only thing I'm unsure of is how to use a view to display the return data.

I made a model and tried to put the data in it, but when I debug it, the data is still there, but I'm not sure what it should return in this line:

 return View(customerModel);

I'm receiving the following 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'.

The technique that invokes the saved process is as follows:

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 the example is:

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 for the auto-populated view.

 @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 modified the View as follows:

 @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>
1
0
11/19/2016 9:10:08 AM

Accepted Answer

ZZZ_tmp
2
3/4/2015 6:54:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow