0

I have an action method which returns a partial view to an ajax call. Whenever the partial view is received in success block of the ajax call, I am rendering it like this;

 success: function (res) {
                $('#competitorTables').html(res);
            }

But the action method has a condition within itself and it can result either true or false. I want to be able to return this boolean value with my return PartialView()block and then I want to be able to process it in my ajax call like this;

 success: function (res, isConditionSatisfied) {
                if(isConditionSatisfied){
                   $('#competitorTables').html(res);
                   alert('Condition satisfied');
                }else{
                   $('#competitorTables').html(res);
                   alert('Condition did not satisfied');
                }
            }

Is there a way that I can manage it? Thanks in advance.

Addition: Here is how I return from my action method;

return PartialView("_CompetitorTables", model);
Ozan Yurtsever
  • 1,274
  • 8
  • 32
  • you can not get partial view as well as data, because if you return something from controller you will get it inside partial view not in your ajax response. – Vishal modi Nov 13 '19 at 11:08

3 Answers3

0

The best way that i suggest is creating a view-model that contains your Model + Your Condition.

Just Like this :

public class TestViewModel
{
   public Model model {get;set;}
   public bool condition {get;set;}
}

And in your controller :

var viewModel = new TestViewModel();
viewModel.condition = true;
return PartialView("_CompetitorTables", viewModel);
Ilia Afzali
  • 429
  • 2
  • 6
0

You can't get value with partial view. The only solution you can do is to create a view model like following and saving partial view response in string and return that view model from your action. Then your code be like :

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');
            }
        }

you can view following link to convert partial view to string: Partial View to string

Atif Aziz
  • 71
  • 7
0

I needed to do a similar thing; make an ajax call and render a partial view as well as return a decimal value to be used for another purpose.

Here's how approached the problem.

  • I added the value I needed to the partial view model
  • I created a hidden input somewhere in the partial view to hold this value
  • Then I made my ajax call and on success, I render the partial view and also used jQuery to fetch the value of that hidden field and voila!
Yusuff Sodiq
  • 815
  • 2
  • 12
  • 19