将数据从控制器返回到VIEW

时间:2014-01-02 05:54:15

标签: asp.net-mvc c#-4.0 razor

我被告知使用旧的传统SQL查询等在MVC 3中创建登录系统,我创建了登录但问题是我将数据从模型返回到View。我已经在模型中创建了数据表并将其返回到控制器,但是不知道如何在视图中显示该数据?搜索得很好但没有帮助。

模特:

public ConnectionStatus Login_db(String email, String pwd, String conStr)
{
    String hashedpwd_login = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "SHA1");
    using (SqlConnection sqlCon = new SqlConnection(conStr))
    {
        using (SqlCommand sqlCom = new SqlCommand())
        {
            sqlCom.Connection = sqlCon;
            sqlCom.CommandText = "select Count(*) from tblRegister where userEmail=@email AND userPwd=@pwd";
            sqlCom.Parameters.AddWithValue("@email", email);
            sqlCom.Parameters.AddWithValue("@pwd", hashedpwd_login);
            String select_com = "select * from tblRegister";
            SqlCommand sqlCom2 = new SqlCommand(select_com, sqlCon);
            ConnectionStatus connectStatus = new ConnectionStatus();
            int no_rows_affected;
            SqlDataAdapter sda = new SqlDataAdapter(select_com, sqlCon);
            DataTable data_tb = new DataTable();

            try
            {
                sqlCon.Open();
                no_rows_affected = Convert.ToInt32(sqlCom.ExecuteScalar());
                if (no_rows_affected == 1)
                {
                    connectStatus.Message = "User logged in successfully, " + no_rows_affected;
                    sda.Fill(data_tb);
                    tableCreation tb_creation = new tableCreation();
                    tb_creation.CreateTable = data_tb;
                }
                else 
                {
                    connectStatus.Message = "Invalid email/password combination.";
                }


            }
            catch (Exception ex)
            {
                connectStatus.Message = ex.Message;
            }
            return connectStatus;

        }
    }
}

控制器:

public ActionResult loginResult(String command, FormCollection formData) 
{
    if (command == "Login")
    {
        var email = formData["txtboxEmail"];
        var pwd = formData["txtboxPassword"];
   //     String conStr = "Data Source=HUNAIN-PC;Initial Catalog=registration;User ID=sa;Password=abc123!@#";
        database model_db = new database();
        var db_status = model_db.Login_db(email, pwd, conStr);
        ViewBag.Message = db_status.Message;


    }
    tableCreation retTable = new tableCreation();
    return View(retTable.CreateTable);
}

查看:

@{
    ViewBag.Title = "Login Authentication";
}

<h2>Login Authentication</h2>

<h4>@ViewBag.Message</h4>

注意:某些类是用户定义的,用于多种目的。

1 个答案:

答案 0 :(得分:0)

您可以在MVC3中使用WebGrid。这是MVC3中的新功能。在您的视图中使用此代码。

@model IList<YourViewModel>
@{
    ViewBag.Title = "Login Information";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    var grid = new WebGrid(source: Model, rowsPerPage: 200, 
    canPage: false, canSort: true, defaultSort: "Absentee");
 }
<p>
    <h2>Absentee List</h2>
        <div id="grid">
            @grid.GetHtml(
                tableStyle: "grid",
                headerStyle: "head",
                alternatingRowStyle: "alt",
                columns: grid.Columns(
                grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", 
                new { id =   item.Id     })), 
                grid.Column("Absentee", "Absentee",canSort:true),
                grid.Column("AbsStart", "AbsStartDate")
            ))
      </div>
  </p>