vineet sharma

vineet sharma

  • NA
  • 22
  • 21.2k

mvc

Mar 10 2014 8:36 AM
how to retrieve a list from database and displaying it in view page using mvc 4 architecture without using ado.net connectivity ?
Controller.cs
Email ed = new Email();
var model = ed.sendemail(Session["username"].ToString()).ToList();
return View(model);
model
email.cs
public List<Email> sendemail(string username)
{
List<Email> tt = new List<Email>();
using (var cn = new SqlConnection(@"Data Source=localhost;Initial Catalog=master;Integrated Security=True"))
{
try
{
string query = @"select receiver1,receiver2,subject,message,dofsen,status from send_email where receiver1='" + username + "' or receiver2='" + username + "'";
cn.Open();
var cmd = new SqlCommand(query, cn);
var reader = cmd.ExecuteReader();
while (reader.HasRows)
{
if (reader.Read())
{ // to , cc, message, subject, sendingdate, status
Email ed = new Email(reader.GetString(0), reader.GetString(1), reader.GetString(2),reader.GetString(3) ,reader.GetDateTime(4), reader.GetString(5));
tt.Add(ed);
}
}
return tt;
}
catch (Exception er)
{
string t = er.ToString();
return null;
}
finally
{
cn.Close();
}
}
}


home.cshtml
@model email.Models.Email

@{
    ViewBag.Title = "SentMail";
    Layout = null;
}
@using (Html.BeginForm())
{
    <div style="width: auto; background-color: #728ea7;">
        @Html.ActionLink("BACK", "Home")
        <span> | </span>
        @Html.ActionLink("COMPOSE MAIL", "ComposeMail")
        <span> | </span>
        @Html.ActionLink("CHANGE PASSWORD", "uchangePassword")
        <span>|</span>
        @Html.ActionLink("UPDATE SECURITY ANSWER", "UpdateSecurityAnswer")
        <span> | </span>
        @Html.ActionLink("Sign Out", "Logout")
    </div>
    <div>
        <fieldset>
            <legend>MESSAGE SENT</legend>
            <!-- //to , cc, message, subject, sendingdate, status-->
            <table border="1">
                <tr>
                    <th>
                        @Html.LabelFor(u => u.To)
                    </th>
                    <th>
                        @Html.LabelFor(u => u.Cc)
                    </th>
                    <th>
                        @Html.LabelFor(u => u.Subject)
                    </th>
                    <th>
                        @Html.LabelFor(u=> u.Message)
                    </th>
                    <th>
                        @Html.LabelFor(u=>u.SendingDate)
                    </th>
                    <th>
                        @Html.LabelFor(u => u.Status)
                    </th>
                </tr>
            @for (int i = 1; i <= ViewBag.tt.Count();i++)
            {
                <!-- //to , cc, message, subject, sendingdate, status-->
                <tr>
                    <td>
                        @Html.TextBoxFor(u=>u.To[i])
                    </td>
                    <td>
                        @Html.TextBoxFor(u => u.Cc[i])
                    </td>
                    <td>
                        @Html.TextBoxFor(u=> u.Subject[i])
                    </td>
                    <td>
                        @Html.TextBoxFor(u => u.Message[i])
                    </td>
                    <td>
                        @Html.TextBoxFor(u => u.SendingDate)
                    </td>
                    <td>
                        @Html.TextBoxFor(u => u.Status[i])
                    </td>
                </tr>
            }
            </table>
        </fieldset>
         </div>
}



Answers (6)