如何在mvc3中附加字符串进行查看?

时间:2012-08-16 09:45:37

标签: asp.net-mvc-3 stringbuilder

我只有一些值...使用它们我在我的控制器中构建一个字符串我想在我的视图页面中显示字符串....

这是我的控制器代码

[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循环在部分视图页面中显示此字符串

1 个答案:

答案 0 :(得分:0)

阿尼尔,

我建议创建一个部分视图(_BugModelList.cshtml),它纯粹处理BugModel的输出显示。这看起来像这样:

@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,但你可以重构它以适应,逻辑保持不变。

[编辑] - 重新阅读您的问题,您可能希望将列表显示为表格,而不是作为无序列表。见上面的编辑