6

I have three partial views on main view Something like this

on the first partial view I have search functionality and when user clicks on search I want to refresh results into 3rd partial view.

Controller:

public ActionResult Search()
{  
         virtualmodel vm = new virtualmodel(); 
      return PartialView(svm);

} 

[HttpPost]
public ActionResult Search(ViewModel svm)
{  
         // Query to retrive the result 
      // I am not sure what to return from here. Link to another action    or just return back to same same partial 

} 

public ActionResult AnotherPartialPartial()
{
}

In main view

 @{Html.RenderAction("Search", "Searchc");
  }

How to do it? Do I need ajax?

5
  • 3
    if you don't want to reload whole page - then you need ajax. If it's ok for you - you can just use regular form post Commented Apr 26, 2016 at 13:07
  • ajax is the easiest way of doing this. Commented Apr 26, 2016 at 13:12
  • most definitely ajax Commented Apr 26, 2016 at 13:13
  • Despite some people really tried to help me but still I am not able to find out workable solution :( Commented Apr 26, 2016 at 14:18
  • @Nil Did you get it resolved - and if you did what was the solution? Commented Feb 12, 2017 at 16:32

4 Answers 4

2

Using ajax you can call a controller action and return it's response to a particular div.

Empty div:

<div class="row" id="div3">

</div>

Ajax to display html in empty div:

function performSearch(searchCriteria) {
   //get information to pass to controller
   var searchInformation = JSON.stringify(**your search information**);

            $.ajax({
                url: '@Url.Action("Search", "ControllerName")',//controller name and action 
                type: 'POST',
                data: { 'svm': searchInformation } //information for search
            })
            .success(function (result) {
                $('#div3').html(result); //write returned partial view to empty div
            })
            .error(function (xhr, status) {
                alert(status);
            })
        }
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I am unable to understand what to put next to data
svm is an object at my action result, do I need to refer to add that object?
sorry yes, I was trying to make it closer to your problem and I missed that part, updating now
it looked like your search action was taking svm as a parameter, which is why I put that there. What is the action you are calling to perform your search, and return the partial view?
I am calling SearchMenu actionresult to get search result and I am passing that result to ListPartial method
|
1

jQuery will help you with it! Try to handle submit button onclick event like this:

$("#yourButtonId").click(function()
{
   $.ajax({
      type: "POST",
                url: "/yourUrl", //in asp.net mvc using ActionResult
                data: data,
                dataType: 'html',
                success: function (result) {
              //Your result is here
              $("#yourContainerId").html(result);
      }
   });
});

4 Comments

I don't know why but it didn't work for me.My be my button is on partial view and div on main view
@Nil Try to add load or reload window.reload(); function after $("#yourContainerId").html(result); if it helps pls, check up as fixes thanx
I wrote it like $("#showFloors").html(result); window.reload(); but still no
@Nil Here is you may get some information about this solution ;) stackoverflow.com/questions/16014129/…
1

You can do it with ajax.

First, change your html.beginform to ajax.beginform in your view and add div id into UpdateTargetId that you want to change contents. After updating first partial with ajax.beginform, you can update other partialviews with ajax.beginform's "OnSuccess" function. You have to add update function like that:

@using (Ajax.BeginForm("YourAction", "YourController", 
        new { /*your objects*/ }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, 
        UpdateTargetId = "ChangeThisPart", OnSuccess = "OnSuccessMethod" }))
        {
           /*your code*/
        }

<script>
   function OnSuccessMethod() {
      $("#YouWantToChangeSecondDivID").load('/YourController/YourAction2');
      $("#YouWantToChangeThirdDivID").load('/YourController/YourAction3');
   };
</script>

Then in your controller, return a partial view to refresh your view part that you entered it's ID in UpdateTargetId value:

public ActionResult YourControllerName(YourModelType model)
{
   ...//your code
   return PartialView("_YourPartialViewName", YourViewModel);
}

Note: Don't forget to add reference to "jquery.unobtrusive-ajax.min.js" in your view while using ajax.

6 Comments

But I have 3 partial views. I want to update 3rd partial for now and later periodically I want to update 2nd partial view, will it still work?
I have updated my answer for updating multiple partial views.
Objects? Which object? One which will be passed to controller ?
Yes. If you want to add additional object to pass to controller, you can add them in that field. But if you don't want it, you don't have to use it.
I tried something like this "@using (Ajax.BeginForm("SearchMenu", "Search", new { /*your objects*/ }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "ChangeThisPart", OnSuccess = "OnSuccessMethod" })) { Html.RenderAction("SearchMenu", "Search"); <div id="abc"></div> } <script> function OnSuccessMethod() { $("#abc").load('/Search/SearchMenu'); $("#YouWantToChangeThirdDivID").load('/YourController/YourAction3'); }; </script>" It didn't work tho
|
0

So, say you have your View with PartialView, which have to be updated by button click:

<div class="target">
 @{ Html.RenderAction("UpdatePoints");}
</div>

<input class="button" value="update" />

There are some ways to do it. For example you may use jQuery:

<script type="text/javascript">
    $(function(){    
        $('.button')click(function(){        
            $.post('@Url.Action("PostActionToUpdatePoints", "Home")').always(function(){
                $('.traget').Load('/Home/UpdatePoints');        
            })        
        });
    });        
</script>

PostActionToUpdatePoints is your Action with [HttpPost] attribute, which you use to update points

If you use logic in your action UpdatePoints() to update points, maybe you forgot to add [HttpPost] attribute to it:

[HttpPost]
public ActionResult UpdatePoints()
{    
    ViewBag.points =  _Repository.Points;
    return PartialView("UpdatePoints");
}

2 Comments

Hey, thanks, I am trying to do it in this way, but here is a new error now, for @{Html.RenderAction("SearchMenu", "Search");} it gives an error System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper
I don't know where am I wrong but it says null exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.