[英]Returning partial view with a boolean value
我有一個操作方法,它將部分視圖返回到 ajax 調用。 每當在 ajax 調用的成功塊中收到部分視圖時,我都會這樣渲染它;
success: function (res) {
$('#competitorTables').html(res);
}
但是動作方法本身有一個條件,它可以導致真或假。 我希望能夠用我的return PartialView()
塊返回這個 boolean 值,然后我希望能夠像這樣在我的 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);
我建議的最好方法是創建一個包含您的 Model + 您的條件的視圖模型。
像這樣:
public class TestViewModel
{
public Model model {get;set;}
public bool condition {get;set;}
}
在您的 controller 中:
var viewModel = new TestViewModel();
viewModel.condition = true;
return PartialView("_CompetitorTables", viewModel);
您無法通過局部視圖獲得價值。 您可以做的唯一解決方案是創建一個視圖 model 就像跟蹤並在字符串中保存部分視圖響應並從您的操作中返回該視圖 model。 然后你的代碼就像:
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');
}
}
您可以查看以下鏈接將部分視圖轉換為字符串: 部分視圖到字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.