Pages

Wednesday, 21 February 2018

MVC Data Reader with Razor


Store Procedure


USE [Girfa_StudentDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[GetStudentList]
AS
BEGIN
     SET NOCOUNT ON;
           select roll,name,city from dbo.Student

     SET NOCOUNT OFF;
END


Model


public class StudentModel

{
    public string Roll { get; set; }
    public string Name { get; set; }
    public string City {get;set;}
}

Controller


public ActionResult Invoice()
{
    Repository ob = new ReportRepository();
    List<StudentModel> list = new List<StudentModel>();
    list=ob.GetInvoiceList();
    return View(list);
}

Repository



public List<StudentModel> GetStudentList(int roll)
{
    List<StudentModel> Record=new List<StudentModel>();
    try
    {
        using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
        {
            using (SqlCommand cmd = new SqlCommand("[dbo].[GetCompleteServiceList]", dbcon))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@roll", id);
    
                if (dbcon.State == ConnectionState.Closed)
                    dbcon.Open();
                using (SqlDataReader drGetRequestedPin = cmd.ExecuteReader())
                {
                    if (drGetRequestedPin.HasRows)
                    {
                               
                        while (drGetRequestedPin.Read())
                        {
                            StudentModel data = new StudentModel();
                            data.Roll = drGetRequestedPin.IsDBNull(0) ? "0" : Convert.ToString(drGetRequestedPin[0]);
                            data.Name = drGetRequestedPin.IsDBNull(1) ? "-" : Convert.ToString(drGetRequestedPin[1]);
                            data.City = drGetRequestedPin.IsDBNull(2) ? "0" : Convert.ToString(drGetRequestedPin[2]);
                                  
                            Record.Add(data);
                        }
                    }
                }
            }
        }
    }                       
    catch (Exception ex)
    {

    }
    return Record;
}

View


@model IEnumerable<Britanic_MIS.Models.InvoiceModel>
<table >
        <thead>
            <tr><th>Roll</th><th>Name</th><th>City</th></tr>
        </thead>
        <tbody >
                 
                @foreach (var item in Model)
                {
                <tr>
                    <td>
                        @Html.DisplayFor(modelitem => item.Roll)
                    </td>
                    <td class="center">
                        @Html.DisplayFor(modelitem => item.Name)
                    </td>
                    <td class="center">
                        @Html.DisplayFor(modelItem => item.City)
                    </td>
                 
                </tr>
                }

        </tbody>

</table>

No comments:

Post a Comment