コントローラー・コード
//pQ is your query you have created
//P4DAL is the key name for connection string
DataSet ds = pQ.Execute(System.Configuration.ConfigurationManager.ConnectionStrings["Platform4"].ConnectionString);
//ds will be used below
//create your own view model according to what you want in your view
//VMData is my view model
var _buildList = new List<VMData>();
{
foreach (DataRow _row in ds.Tables[0].Rows)
{
_buildList.Add(new VMData
{
//chose what you want from the dataset results and assign it your view model fields
clientID = Convert.ToInt16(_row[1]),
ClientName = _row[3].ToString(),
clientPhone = _row[4].ToString(),
bcName = _row[8].ToString(),
cityName = _row[5].ToString(),
provName = _row[6].ToString(),
});
}
}
//you will use this in your view
ViewData["MyData"] = _buildList;
ビュー
@if (ViewData["MyData"] != null)
{
var data = (List<VMData>)ViewData["MyData"];
<div class="table-responsive">
<table class="display table" id="Results">
<thead>
<tr>
<td>Name</td>
<td>Telephone</td>
<td>Category </td>
<td>City </td>
<td>Province </td>
</tr>
</thead>
<tbody>
@foreach (var item in data)
{
<tr>
<td>@Html.ActionLink(item.ClientName, "_Display", new { id = item.clientID }, new { target = "_blank" })</td>
<td>@item.clientPhone</td>
<td>@item.bcName</td>
<td>@item.cityName</td>
<td>@item.provName</td>
</tr>
}
</tbody>
</table>
</div>
}
あなたは、任意のドメインオブジェクトを持っていますか?私はあなたのコントローラ内のドメインオブジェクトにDataTableを翻訳し、それらをビューに渡すことに傾きます。 –
これは最善のアプローチではありませんが、古いアプリを移行するので、データセットをあたかもモデルエンティティのようにビューに渡すことができます。つまり、あなたのモデルはデータセットになります。 – Romias
オブジェクトにGetとSetsを使用していますか?うーん。私は200列のクエリをいくつか持っています。私は手で書くのは時間がかかると思う。他に何かありますか? – tdjfdjdj