views:

962

answers:

6

Hello, I'm with a problem, I have a ajax link that pass a parameter, but, the page that it opens does not need that parameter. The page only load 2 partial views, one of those need that parameter passed to the page to load the data correctly, and the other just need to load a form, so, don't need that parameter. How can i acheive this?

A: 

No one can answer?

Could your provide the code from your controller ?
eu-ge-ne
A: 

//Controller

  public ActionResult EditFunctions(int id)
    {

        var sysfunctions= UnisegurancaService.FunctionsRepository.All();
        return View(sysfunctions);
    }
    // This is the controller (it does no need the parameter "ID")

//This is the view "EditFunctions"

<div id="formFunction">
<% Html.RenderPartial("FormFunction"); %>
</div>


<div id="gridFunction">
<% Html.RenderPartial("GridFunction"); %> // The grid needs the ID to work correctly but its in the parent page not in the partial call....and the call is an ajax call
</div>
Is your GridFunction partial view strongly typed ?
eu-ge-ne
+1  A: 

In order to do what you want, you will need to add the id to the ViewData construct.

var sysfunctions= UnisegurancaService.FunctionsRepository.All();
ViewData["NeededID"] = id
return View(sysfunctions);

then in your view where you render the partial

<%= Html.RenderPartial("GridFunction", (int)ViewData["NeededID"]) %>

Cast as required of course.

Whatever gets pushed in as the second param becomes the .Model in the partial. I would suggest also strongly typing your partials.

Chad Ruppert
Sorry...type mistake..corrected
No problem. I've made my answer more helpful. If you don't wish to strongly type your partials (which I would suggest you do type them) then the advice by eu-ge-ne with the ViewDataDictionary is better.
Chad Ruppert
A: 

Try this:

<% Html.RenderPartial("GridFunction", new ViewDataDictionary {{"Id", ViewData["Id"]}}); %>

UPDATED:

And add this in your controller action:

ViewData["Id"] = Id;

UPDATED:

And in your GridFunction partial View you can access Id as:

<%= ViewData["Id"] %>
eu-ge-ne
A: 

If some dependency of the page needs the parameter, then the page needs to know enough to pass the data in, so the page should be able to provide the data. Or, more simply, just add the parameter to the Page's viewdata and be done with it.

Wyatt Barnett
A: 

Thanks for all....awesome, problem solved.

Can you mark the most correct answer as accepted?
Chad Ruppert