Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 - Ajax - Replace one partial one view with another

I'm trying to replace the contents of a div in a Main view with partial views depending on which @Ajax.ActionLink is clicked.

Initally I load the _CaseLoad partial view then I would like to be able to switch between the two partial views _CaseLoad and _Vacancies when I click on the relevant ActionLink. I have bothe partial views and the Index view in the 'Main' folder.

When I click on the 'Vacancies' link it briefly shows the LoadingElementID but it does not replace the _CaseLoad partial view with _Vacancies partial view?

Controller Action (MainController):

    public ActionResult Index(int page = 1, string searchTerm = null)
    {
        MainIndexViewModel model = new MainIndexViewModel()
        // Populate Model

        return View(model);

    }

Main Index View:

@model Project.WebUI.Models.MainIndexViewModel

@{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="MainIndex">
        <div id="Menu">

            @Ajax.ActionLink("CaseLoad", "_CaseLoad", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
                LoadingElementId = "busycycle" }) |

            @Ajax.ActionLink("Vacancies", "_Vacancies", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
                LoadingElementId = "busycycle" })

        </div><hr/>


        <div id="busycycle" style="display:none;"><img src="~/Content/images/preloader-w8-cycle-black.gif" /></div>
        <div id="Main">
            @Html.Partial("_CaseLoad")
        </div>

    </div>
like image 666
James P Avatar asked Apr 18 '13 12:04

James P


People also ask

How do I update multiple partial views in MVC?

MVC – Updating multiple partial views from a single (AJAX) action. Sometimes you need to make an AJAX call from your MVC view to perform some server side actions on user changes. You may then want to return some calculated data in a partial view so that you can update the view as presented to the user.

Can I return a partial view from a AJAX call?

The cool thing is that you can also return partial Views to Ajax calls. Here's a getJson call to a ASP.MVC Controller's Action method that expects to get back a set of HTML that it inserts into a page's div element:

Can We keep the code in a partial view?

If this is the situation then we can keep the code in a partial view and the great advantage is that, once we change to a partial view, it will affect all pages, no need to change page to page. So, let's create one simple MVC application and try to return a partial from controller and display it using jQuery AJAX.

Why do I need to return data from a partial view?

Sometimes you need to make an AJAX call from your MVC view to perform some server side actions on user changes. You may then want to return some calculated data in a partial view so that you can update the view as presented to the user. What if you have modified data in more than one partial view?


1 Answers

Your call to @Ajax.ActionLink is wrong, you are doing this:

@Ajax.ActionLink("CaseLoad", "_CaseLoad", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
            LoadingElementId = "busycycle" })

Which means your link text is "CaseLoad", and the action to be called in "MainController" is "_CaseLoad"? Isn't "_CaseLoad" the name of your partial view?

To fix this, you must create CaseLoad and a Vacancies actions in your MainController, make both methods return PartialView("_CaseLoad.cshtml", model); or PartialView("_Vacancies.cshtml, model) like below:

public ActionResult CaseLoad(int page = 1, string searchTerm = null)
{
    MainIndexViewModel model = new MainIndexViewModel()
    // Poopulate Model

    return PartialView("_CaseLoad.cshtml", model);
}

I just cut off all implementation details just to get you an idea about how to fix the issue.

like image 170
ararog Avatar answered Oct 12 '22 07:10

ararog