Newbie
Newbie

Reputation: 361

Read data from model and display on razor view - mvc

I am trying to get the data from database through model and display it on a view. Here is the sample code, i am able to pull the data but that is not showing up on the view.I appreciate any help. Thanks in advance

Model Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace Test.Models
{
    public class CustModel
    {
        SqlConnection con = new SqlConnection();        
        List<CustModel> CustList = new List<CustModel>();       
        public String CustName { get; set; }        
        public String CustPhone { get; set; }         
        CustModel p = null;        
        public List< CustModel > GetCustInfo()        
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CustConnectionString"].ToString());
            con.Open();
            using (con)            
            {
            SqlCommand cmd = new SqlCommand("select * FROM Cust_tb where ZIP = 37771", con);               
            SqlDataReader rd = cmd.ExecuteReader();               
            while (rd.Read())               
            {                   
                    p = new CustModel ();
                    p.CustName =Convert.ToString(rd.GetSqlValue(0));
                    p.CustPhone = Convert.ToString(rd.GetSqlValue(10));                    
                    CustList.Add(p);               
            }                        
            return CustList;
      }
   }
}

Controller Code

public ActionResult CustDisplay()
    {
        CustModel p = new CustModel();
        List<CustModel> Li = new List<CustModel>();
        Li = p. GetCustInfo (); 
        ViewData["CustInfo"] = Li; 
        return View("CustDisplay");
    }

View Code

@model Test.Models.CustModel

@{
    ViewBag.Title = "CustDisplay";
}

<!DOCTYPE html>
    <head>
        <title> Customer Information </title>
    </head>
    <body>
        <div class="display-label">
            Name
        </div>
        <div class="display-field">
            @Html.DisplayFor(Model => Model.CustName)
        </div>
        <div class="display-label">
            Phone
        </div>
        <div class="display-field">
            @Html.DisplayFor(Model => Model. CustPhone)
        </div>
    </body> 
</html>

Upvotes: 2

Views: 43611

Answers (1)

RandomShelly
RandomShelly

Reputation: 276

Maybe I am over-simplifying this - but I think it is just because you didn't actually send or use your list in the view... I don't get my data the way you did - so I will assume that you are getting the data you want, and the returning list contains 1 to many rows of data in p (name and phone)

even though you pass your list in ViewData - you aren't assigning it in the view.. the best way to do this would be in your controller to call return View(Li); and not return View("CustDisplay"); you are already in the view you want to call - so it will send the list into the @model. (There may be a way to assign the ViewData to the model in the view also) - but I pass the list in, so the data is bound...

You should make the model @model IEnumerable<Test.Models.CustModel>

and then

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CustName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CustPhone)
        </th>                        
    </tr>

    @foreach (var item in Model){
    <tr>   
        <td>
            @Html.DisplayFor(modelItem => item.CustName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CustPhone)
        </td>                    
    </tr>
    }
</table>

Upvotes: 3

Related Questions