我正在尝试将一个ajax响应数组推入MVC表中。 这是我的脚本的样子:
这是我的部分视图的样子:
这是WebApi方法的样子:
<script type="text/javascript">
$(document).ready(function () {
$('#form1').submit(function (event) {
event.preventDefault();
event.returnValue = false;
var selectValue = $('#selectValue').val();
$.ajax({
url: "/api/Admin/GetDepotDetails/",
type: "POST",
data: { "selectValue": selectValue },
dataType: "json",
success: function (data) {
$("#Grid").html($(data).children());
},
error: function (jqXHR, textStatus, errorThrown) {
debugger;
alert(textStatus, errorThrown, jqXHR);
}
});
});
});
</script>
这是我的部分视图的样子:
@model IEnumerable<SampleApp.DepotDetail>
<table class="table table-condensed" id="Grid">
<tr>
<th>
@Html.DisplayNameFor(model => model.DepotID)
</th>
<th>
@Html.DisplayNameFor(model => model.ColumnName)
</th>
<th>
@Html.DisplayNameFor(model => model.Country)
</th>
<th>
@Html.DisplayNameFor(model => model.CountryCode)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr class="warning">
<td>
@Html.DisplayFor(modelItem => item.DepotID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ColumnName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountryCode)
</td>
</tr>
}
</table>
这是WebApi方法的样子:
[HttpPost]
public IEnumerable<DepotDetail> GetDepotDetails(Selected selectValue)
{
var model = depotsDetails.Where(x => x.ColumnName == selectValue.SelectValue) as IEnumerable<DepotDetail>;
var viewModel = new SearchViewModel{ DepotDetailsList = model, ActionLists = new ActionList()} ;
return model;
}
这是视图的样子:
@model IEnumerable<SiemensSampleApp.DepotDetail>
<div class="row">
<div class="col-md-4">
<form id="form1">
@*@Html.DropDownListFor(model => model.DepotListSelectValue, Model.DepotLists, new { @class = "form-control" })*@
@Html.DropDownList("selectValue", new List<SelectListItem>
{
new SelectListItem() {Text = "Depot ID", Value="Depot ID"},
new SelectListItem() {Text = "Depot Name", Value="Depot Name"},
new SelectListItem() {Text = "Address", Value="Address"}
}, new { @class = "selectValue", @id = "selectValue" })
@*//, new { @class = "chzn-select", @id = "chzn-select" }*@
<input type="submit" value="submit" />
</form>
<br /><br /><br />
<table id="records_table" style="width:100%;"></table>
<div id="tableHere">
@{
if (Model == null)
{
Html.RenderPartial("_SearchResult", new List<DepotDetail>() { });
}
}
</div>
</div>
</div>
问题:我正在尝试通过WebApi获取详细信息列表并将其绑定到MVC表格。怎样做最好?
我使用了下面的代码来填充Grid,但是表格没有任何数据。请问有没有人能够给我一个使用上述部分视图来填充Grid的想法。非常感谢!
$("#Grid").html($(data).children());