如何从控制器获取数据到Html [英] How Do I Get Data From Controller To Html

查看:392
本文介绍了如何从控制器获取数据到Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器中:



In Controller:

public ActionResult Employee()
       {
           //List<CustomerInfo> oCustomerInfos = DataLayer.GetALLCustomers();
           List<Employee> oEmpInfos = DB.GetALLEmpDetails();

           var myResult = new
           {
               //CustomerList = oCustomerInfos,
               ProductList = oEmpInfos

           };
           return View(oEmpInfos); // return list of employee


       }





数据层:





in Data Layer:

public static List<employee> GetALLEmpDetails()
       {
           try
           {
               string connections = System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeContext"].ToString();
               SqlConnection conn = new SqlConnection(connections);
               string query = "select * from Employee";

               SqlCommand cmd = new SqlCommand(query, conn);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dtEmployees = new DataTable();
               da.Fill(dtEmployees);

               List<employee> oEmpInfos = new List<employee>();
               if (dtEmployees != null && dtEmployees.Rows.Count > 0)
               {
                   foreach (DataRow dr in dtEmployees.Rows)
                   {
                       Employee oEmpInfo = new Employee();
                       oEmpInfo.EmpId = Convert.ToInt32(dr["EmpId"]);
                       oEmpInfo.EmpName = dr["EmpName"].ToString();
                       oEmpInfo.Designation = dr["Designation"].ToString();
                       oEmpInfo.Percentage1 = Convert.ToInt32(dr["Percentage1"]);
                       oEmpInfo.Percentage2 = Convert.ToInt32(dr["Percentage2"]);
                       oEmpInfo.Location = dr["Location"].ToString();
                       oEmpInfos.Add(oEmpInfo);
                   }
               }
               return oEmpInfos;
           }

推荐答案

使用viewdata或viewbag -

什么是ViewData,ViewBag和TempData? - 在当前和后续请求之间传递数据的MVC选项 [ ^ ]

在ASP.NET MVC应用程序中传递数据 [ ^ ]
Use viewdata or viewbag -
What is ViewData, ViewBag and TempData? – MVC Options for Passing Data Between Current and Subsequent Request[^]
Passing Data in an ASP.NET MVC Application[^]


好,你在action方法中有数据。现在将该数据绑定到它的视图。



Good, you have data in the action method. Now bind that data to its view.

@model IEnumerable<Employee>

<ul>
@foreach(var item in Model)
{
    <h3>@item.EmpName</h3>
    <p>Designation: @item.Designation
    <br />Location: @item.Location
    </p>
    
    <hr />
}
</ul>



这将列出您的所有员工详细信息。 :)



请参阅 -

在ASP.NET MVC中传递数据 [ ^ ]

将数据从Controller传递到ASP.NET MVC中的视图 [ ^ ]





-KR


This will list out your all the employee details. :)

Refer this-
Passing Data in an ASP.NET MVC[^]
Passing data from Controller to View in ASP.NET MVC[^]


-KR


这篇关于如何从控制器获取数据到Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆