[英]jQuery ajax response bind into MVC Model table
我正在嘗試將ajax響應數組推入MVC表。 這是我的腳本的樣子:
<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;
}
這就是View的樣子:
@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表。 做這個的最好方式是什么? 我用過
$( “#網格”)HTML($(數據)。兒童());
填補網格。 但該表沒有任何數據。 有人可以讓我知道如何使用上面的部分視圖填充網格。 先感謝您!
您的web api端點返回數據(采用json格式),而不是部分視圖中的HTML標記。 你有2個選擇。
1)創建一個mvc動作方法,該方法獲取數據並將其傳遞給局部視圖並返回部分視圖響應並使用它來更新UI
[HttpPost]
public IEnumerable<DepotDetail> GetDepotDetails(Selected selectValue)
{
var model = depotsDetails.Where(x => x.ColumnName == selectValue.SelectValue)
as IEnumerable<DepotDetail>;
return PartialView(model);
}
現在確保在~/Views/Shared
或~/View/YourControllerName/
有一個名為GetDepotDetails.cshtml的局部視圖。 這個視圖應該強烈地輸入到DepotDetail的集合中。
@model IEnumerable<DepotDetail>
<p>Here loop through each item in Model and render the table</p>
<p> Same as your partial view in the question </p>
在你的成功活動中,
success: function (data) {
$("#Grid").html(data);
},
2)使用當前的web api端點並讀取ajax方法success
事件中的數據,並動態構建表行的html標記,並將其設置為Grid表的內容。
success: function (data) {
var tblHtml="";
$.each(data.DepotDetailsList,function(a,b){
tblHtml+= "<tr><td>"+b.DepotID+"</td>";
tblHtml+= "<td>"+b.ColumnName+"</td>";
tblHtml+= "<td>"+b.Country+"</td>";
tblHtml+= "<td>"+b.CountryCode+"</td>M/tr>";
});
$("#Grid > tbody").html(tblHtml);
},
由於您已經擁有構建表的局部視圖,因此您可以執行此操作。
在您的ajax success方法中,通過傳遞從API接收的數據來調用控制器操作。 控制器將通過傳遞相同的模型返回現有的局部視圖。
$.ajax({
url: "/api/Admin/GetDepotDetails/",
type: "POST",
data: { "selectValue": selectValue },
dataType: "json",
success: function (data) {
//$("#Grid").html($(data).children());
$.get("controller/Action",data.DepotDetailsList ,function(response){
$('#tableHolder').html(response) //have a div in your main view which will hold the table. Now the partial view has to be replaced into this div.
});
},
error: function (jqXHR, textStatus, errorThrown) {
debugger;
alert(textStatus, errorThrown, jqXHR);
}
});
正如我所說,創建一個新的MVC操作方法並通過傳遞從ajax發送的模型返回相同的局部視圖。
或者您可以使用Jquery並再次構建表 - 但如果表HTML很大(使用css,屬性,動態到達等),這將是一個痛苦。 - 我認為另一個答案已經有了詳細說明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.