从我的控制器获取数据列表并将其显示在我的视图中

时间:2014-05-07 10:26:35

标签: html5 asp.net-mvc-4 razor

这是我的模型

namespace Maintenance_.Models
{
    public class IndexModel
    {
        public IList<Maintenance_.Models.IndexModel> fNameList { get; set; }
    }
}

这是我的控制器

[HttpPost]
public ActionResult ProcessTech()
{
    TechnicianFacade _oTechFacade = new TechnicianFacade();
    Technician _oTechnician = new Technician();
    Maintenance_.Models.IndexModel _oTechModel = new Maintenance_.Models.IndexModel();
    IList<Maintenance_.Models.IndexModel> _otechList = new List<Maintenance_.Models.IndexModel>();
    var tech = _oTechFacade.getTechnicians("", _oAppSetting.ConnectionString).ToArray();
    foreach (var test in tech)
    {
        string fName = test.GetType().GetProperty("FIRSTNAME").GetValue(test, null).ToString();
        _oTechModel.firstName = fName;
        _otechList.Add(_oTechModel);
    }
    _oTechModel.fNameList = _otechList;
    return View("Index", _oTechModel);  
}

我的控制器已经可以保存我的_otechList

中的数据列表

我的观点

@model Maintenance_.Models.IndexModel
@{
    ViewBag.Title = "Technician";    
}

<div id="page-wrapper">
<div class = "row">
    <div class = "col-lg-12">
        <h1 class= "page-header"> Technician </h1>  
    </div>
</div>
<div class = "row">
<center>           
        ...@*My Ajax code*@...
            <div style ="width:300px;height:500px; float:left;margin-left:40px;">
            <table style ="float:left;border:5px solid;">
                <thead>
                    <tr>
                        <td style ="background-color:#3ba7f7; height:40px;width:100px; border-bottom:5px solid;">Technician No.</td>
                        <td style ="background-color:#3ba7f7; height:40px;width:200px; border-bottom:5px solid;">Technician Name:</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td style ="background-color:#E0EBEB; height:40px;width:100px;"><a href ="#" onclick="call();" style ="text-decoration:none; text-decoration-color:black;"> T-00000031</a></td>
                 ==>    <td style ="background-color:#E0EBEB; height:40px;width:200px;"> Jerald Villaceran</td>
                    </tr>
                    </tbody>
                </table>    
                </div>                       
...

现在,我想更改我输入“==&gt;”的代码行成为动态类型的代码, 我还可以在我的td标签或tr标签中使用foreach吗?就像我在控制器中从数据库中获取所有数据一样。或者我可以使用@html.displaytext()?

2 个答案:

答案 0 :(得分:0)

在您看来,您需要进行以下更改:

@model IList<Maintenance_.Models.IndexModel>
@{
    ViewBag.Title = "Technician";    
}

@foreach(var item in Model)
{
 <tr>
 <td style ="background-color:#E0EBEB; height:40px;width:100px;"><a href ="#" onclick="call();" style ="text-decoration:none; text-decoration-color:black;"> T-00000031</a></td>
 <td style ="background-color:#E0EBEB; height:40px;width:200px;"> @item.FirstName</td>
 </tr>
}

答案 1 :(得分:0)

试试这个。它仍将使用IndexModel作为您的模型。你只需循环遍历fNameList。

 @model Maintenance_.Models.IndexModel

    @{
        ViewBag.Title = "Technician";    
    }
    <table style ="float:left;border:5px solid;">

    @foreach(var item in Model.fNameList)
    {
     <tr>
     <td style ="background-color:#E0EBEB; height:40px;width:100px;"><a href ="#" onclick="call();" style ="text-decoration:none; text-decoration-color:black;"> T-00000031</a></td>
     <td style ="background-color:#E0EBEB; height:40px;width:200px;"> item.FirstName</td>
     </tr>
    }
    </table>
相关问题