2012-08-16 20 views
1

大家好我有一些值...使用它們我建立我的控制器字符串i灣在我看來,頁面顯示的字符串....如何追加一個字符串在mvc3中查看?

這裏是我的控制器代碼

[ChildActionOnly] 
public ActionResult History() 
{ 
    //if(Session["DetailsID"]!=null) 
    //{ 
    int id = Convert.ToInt16(Session["BugID"]); 
     var History = GetBugHistory(id); 
     return PartialView(History); 
    //} 
    //int id1 = Convert.ToInt16(Session["BugID"]); 
    //var History1 = GetBugHistory(id1); 
    //return PartialView(History1); 

} 
/// <summary> 
///To the List of Resolutions and Employeenames Based on BugID 
/// </summary> 
/// <param>Bug Id</param> 
/// <returns>BugHistory</returns>  
public List<BugModel> GetBugHistory(int id) 
{ 

    var modelList = new List<BugModel>(); 
    using (SqlConnection conn = new SqlConnection(ConnectionString)) 
    { 
     conn.Open(); 
     SqlCommand dCmd = new SqlCommand("History", conn); 
     dCmd.CommandType = CommandType.StoredProcedure; 
     dCmd.Parameters.Add(new SqlParameter("@BugID", id)); 
     SqlDataAdapter da = new SqlDataAdapter(dCmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     StringBuilder sb = new StringBuilder(); 
     conn.Close(); 
     for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
     { 
      var model = new BugModel(); 
      model.FixedBy = ds.Tables[0].Rows[i]["FixedByEmployee"].ToString(); 
      model.Resolution = ds.Tables[0].Rows[i]["Resolution"].ToString(); 
      model.AssignedTo = ds.Tables[0].Rows[i]["AssignedEmployee"].ToString(); 
      model.Status = ds.Tables[0].Rows[i]["Status"].ToString(); 
      model.ToStatus= ds.Tables[0].Rows[i]["ToStatus"].ToString();     
      modelList.Add(model); 
      sb.Append("'" + model.FixedBy + "'" + "has updated the status from" + "'" + model.ToStatus + "'" + "to" + "'" + model.Status + "'" + "and Assigned to" + "'" + model.AssignedTo + "'"); 
     } 
     return modelList; 
    }   
} 

我應該如何顯示使用foreach循環

回答

0

阿尼爾這串在我的局部視圖頁面,

我建議創建處理純粹是的輸出顯示一個partialview(_BugModelList.cshtml)。這可能是這個樣子:

@model IList<BugModel> 

<table> 
    <thead> 
     <tr> 
      <th>Fixed by</th> 
      <th>From Status</th> 
      <th>To Status</th> 
      <th>Assigned to</th> 
     </tr> 
    </thead>  
    @{ 
     foreach (var item in Model) 
     { 
      <tr> 
       <td>@item.FixedBy</td> 
       <td>@item.ToStatus</td> 
       <td>@item.Status</td> 
       <td>@item.AssignedTo</td> 
      </tr> 
     } 
    } 
</table> 

,或者,如果你想在一個字符串每控制器(未經測試顯然)爲:

@model IList<BugModel> 

@{ 
    foreach (var item in Model) 
    { 
     <p> 
      @{ "'" + item.FixedBy + "'" 
        + " has updated the status from " 
        + "'" + item.ToStatus + "'" 
        + " to " + "'" + item.Status + "'" 
        + " and Assigned to " + "'" + item.AssignedTo + "'"; } 
     </p> 
    } 
} 

這會再從你的主要觀點被稱爲每你目前擁有的行動。很明顯,我將它格式化爲table,但是您可以重構它以適應邏輯保持不變。

[編輯] - 重新閱讀您的問題,您可能希望將列表呈現爲表格,而不是無序列表。請參閱上面的編輯

+0

以上建議的任何喜悅? – 2012-08-17 16:03:36