やりたいことはこんな感じです。
フォームを送信すると、フォーム内の値がコントローラーに渡され、値が変換されてデータテーブルに返されます。
コントローラーからjsonデータをデータテーブルに返す方法を知っています。
また、Ajax.BeginForm によってフォーム値をコントローラーに渡す方法も知っています。
しかし、私は両方を行う方法がわかりません。
これは、固定jsonデータをデータテーブルに返すだけの私のコードです。
意見
<script type="text/javascript">
function PullIntoTable() {
$('#example').dataTable({
"bDestroy": true,
"bAutoWidth": false,
"aoColumns": [
{ sWidth: '30%' },
{ sWidth: '10%' },
{ sWidth: '20%' },
{ sWidth: '20%' },
{ sWidth: '20%' }, ],
"sAjaxSource": '@Url.Action("Search", "SearchCompany")',
"fnServerData": function (sSource, aoData, fnCallback) {
$.ajax({
dataType: 'json',
type: "POST",
url: sSource,
data: aoData,
success: function (json) {
fnCallback(json);
}
})
}
});
};
</script>
<input type="submit" value="Search" onclick="PullIntoTable()" />
<table cellpadding="0" cellspacing="0" border="0" class="search_result" id="example" width="100%">
<thead>
<tr>
<th>Company</th>
<th>Location</th>
<th>Address</th>
<th>Status</th>
<th>Inactive</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
コントローラ
public class SearchCompanyController : Controller
{
[HttpPost]
public ActionResult Search()
{
var Result = new List<Result>()
{
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"},
new Result{Name="DummyCompany", Location="DummyLocation", Address="DummyAddress", Status="SL", Inactive="Y"}
};
return Json(
new { aaData = Result.Select(x => new[] { x.Name, x.Location, x.Address, x.Status, x.Inactive }) },
JsonRequestBehavior.DenyGet
);
}
}
public struct Result
{
public string Name { get; set; }
public string Location { get; set; }
public string Address { get; set; }
public string Status { get; set; }
public string Inactive { get; set; }
}