Ebleme
Ebleme

Reputation: 307

Passing any data from a partial view that is rendered with $.get() to ContentPage

I am trying to make my web application like a desktop application. I am also not using any _LayoutPage and @RenderBody().

I have a ContentPage as MasterPage and a tag named main. I am using ajax get method to render my views or partial views like this:

  $.get(url).done(function (result) {
            $("main").html(result);
        });

I managed to inject my script and css files with javascript functions.

And now I want to pass some specific datas without using javascript functions.

It can be via using ViewBag, I guess.

I want to pass that data from my partialView:

 ViewBag.BodyClass = "signup-page";

to my MainPage like this:

<body class="@ViewBag.BodyClass">

How can I do that?

Upvotes: 0

Views: 206

Answers (2)

Ebleme
Ebleme

Reputation: 307

I wrote that code on my partialView. It adds a class at ContentPage's body tag

$("body").addClass("signup-page");

Upvotes: 0

Fabio Silva Lima
Fabio Silva Lima

Reputation: 714

If you have a script manager ($.get) that calls your server to get the views and partial views, no problem.

When you request a URL, normally MVC calls a Controller and Action. In that action you can return content, view, partial view, file and so on...

You can create a new instance of a class model and pass to your partial view.

public ActionResult Index(string parameter1, string parameter2)
{
     var model = new Models.ModelTest();
     model.BodyClass = "some class";

     return PartialView("_Page", model);
}

You will call some like this:

$.get("http://localhost/app/getviews?id=3422&parameter1=test&parameter2=foo")

In your view or partial view:

@model YourApp.Models.ModelTest

<body class="@Model.BodyClass">

I use that all the time.

Upvotes: 1

Related Questions