我有一个action方法,可以将部分视图返回给ajax调用。每当在ajax调用的成功块中收到部分视图时,我都会像这样渲染它;
success: function (res) {
$('#competitorTables').html(res);
}
但是操作方法本身具有一个条件,它可以导致true或false。我希望能够使用我的return PartialView()
块返回该布尔值,然后希望能够在像这样的ajax调用中对其进行处理;
success: function (res, isConditionSatisfied) {
if(isConditionSatisfied){
$('#competitorTables').html(res);
alert('Condition satisfied');
}else{
$('#competitorTables').html(res);
alert('Condition did not satisfied');
}
}
有没有办法可以管理它?预先感谢。
添加:这是我从操作方法中返回的方式;
return PartialView("_CompetitorTables", model);
答案 0 :(得分:0)
我建议的最好方法是创建一个包含模型+条件的视图模型。
就像这样:
public class TestViewModel
{
public Model model {get;set;}
public bool condition {get;set;}
}
在您的控制器中:
var viewModel = new TestViewModel();
viewModel.condition = true;
return PartialView("_CompetitorTables", viewModel);
答案 1 :(得分:0)
局部视图无法获得价值。您唯一可以做的解决方案是创建一个视图模型,例如跟随并以字符串形式保存部分视图响应,然后从您的操作中返回该视图模型。然后您的代码就像:
public class Model
{
public string PartialViewResponce {get;set;}
public bool condition {get;set;}
}
success: function (res) {
if(res.Condition){
$('#competitorTables').html(res.PartialViewResponce);
alert('Condition satisfied');
}else{
$('#competitorTables').html(res.PartialViewResponce);
alert('Condition did not satisfied');
}
}
您可以查看以下链接以将部分视图转换为字符串: Partial View to string