[英]MVC Partial View Not Refreshing when using JSON data
I have a dropdown that I'm using to refresh a div with checkboxes. 我有一个下拉列表,用于刷新带有复选框的div。
I am trying to figure out why my view is not refreshing if I pass in data in JSON format. 我试图弄清楚如果我以JSON格式传递数据为什么视图不刷新。 If I pass in just a regular string, the view refreshes.
如果仅传递常规字符串,则视图将刷新。 If I pass in JSON data, the view does not refresh.
如果我传递JSON数据,则视图不会刷新。
If I step through the code in the Partial view, I can see the correct number of records are being passed in, however the view doesn't get refreshed with the correct number of checkboxes. 如果我在Partial视图中单步执行代码,则可以看到传递了正确数量的记录,但是该视图并没有使用正确数量的复选框刷新。
I tried to add some cache directives, it didn't work. 我试图添加一些缓存指令,但是没有用。
This doesn't work: 这不起作用:
$(function () {
$('#ddlMoveToListNames').change(function () {
var item = $(this).val();
var selectedListID = $('#ddlListNames').val();
var checkValues = $('input[name=c]:checked').map(function () {
return $(this).val();
}).toArray();
$.ajax({
url: '@Url.Action("Test1", "WordList")',
type: 'POST',
cache: false,
data: JSON.stringify({ words: checkValues, moveToListID: item, selectedListID: selectedListID }),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (result) {
}
})
.done(function (partialViewResult) {
$("#divCheckBoxes").replaceWith(partialViewResult);
});
});
});
This works: 这有效:
$(function () {
$('#ddlMoveToListNames').change(function () {
var item = $(this).val();
var selectedListID = $('#ddlListNames').val();
var checkValues = $('input[name=c]:checked').map(function () {
return $(this).val();
}).toArray();
$.ajax({
url: '@Url.Action("Test1", "WordList")',
type: 'POST',
cache: false,
data: { selectedListID: item },
success: function (result) {
}
})
.done(function (partialViewResult) {
$("#divCheckBoxes").replaceWith(partialViewResult);
});
});
});
Partial View: 部分视图:
@model WLWeb.Models.MyModel
<div id="divCheckBoxes">
@foreach (var item in Model.vwWordList)
{
@Html.Raw("<label><input type='checkbox' value='" + @Html.DisplayFor(modelItem => item.Word) + "' name='c'> " + @Html.DisplayFor(modelItem => item.Word) + "</label> ");
}
</div>
Controller: 控制器:
[AcceptVerbs(HttpVerbs.Post)]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public PartialViewResult Test1(MyModel vm, string[] words, string moveToListID, string selectedListID)
{
int listNameID = Convert.ToInt32(moveToListID);
List<vwWordList> lst = db.vwWordLists.Where(s => s.Word.StartsWith("wa") && s.ListID == listNameID).ToList();
vm.vwWordList = lst;
return PartialView("Partial1", vm);
}
View: 视图:
@Html.DropDownListFor(x => Model.FilterViewModel.MoveToListNameID, Model.FilterViewModel.MoveToListNameList,
new { @id = "ddlMoveToListNames", style = "width:100px;" })
From jquery page 从jQuery页面
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. dataType(默认值:Intelligent Guess(xml,json,脚本或html))类型:String期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).
如果未指定,则jQuery将尝试根据响应的MIME类型来推断它(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4脚本中,脚本将执行该脚本,而其他任何内容将是以字符串形式返回)。 The available types (and the result passed as the first argument to your success callback).
可用的类型(并将结果作为第一个参数传递给您的成功回调)。
http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/
Using dataType:json expect json and your app returns html 使用dataType:json期望json并且您的应用返回html
Change dataType:'json' to dataType:'html' 将dataType:'json'更改为dataType:'html'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.