1

こんにちはすべて私はいくつかの値を持っています...それらを使用して私は私のコントローラーで文字列を構築しています私は私のビューページに文字列を表示したいです...

これが私のコントローラーコードです

[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ループを使用して、この文字列を部分ビューページに表示するにはどうすればよいですか?

4

1 に答える 1

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たが、それに合わせてリファクタリングできます。ロジックは同じままです。

[編集] - 質問を読み直すと、リストを順序なしのリストとしてではなく、表として表示したいでしょう。上記の編集を参照してください

于 2012-08-16T09:59:18.213 に答える