views:

40

answers:

1

Hi, I'm trying to mimic the webforms multiview functionality and the only way i can think of is to put a PartialView into a ViewData object? Something like the following:

View code:

<%= ViewData["PartialViewPlaceholder"] %>

Controller code:

if(//condition){    
    ViewData["PartialViewPlaceholder"] = partialView1;
} else {
    ViewData["PartialViewPlaceholder"] = partialView2;
}

How would you go about this?

+1  A: 

ViewData is meant to contain actual data, not views themselves, which contain markup and rendering code. Would it not be possible for you to do this:

public ActionResult MyActionMethod()
{
    var model = new MyModel();
    model.UsePartialView1 = false; // Tell the view not to use Partial View 1
    return View("MyView", model);
}    

And in the View MyView:

<% if (Model.UsePartialView1) 
       Html.RenderPartial("PartialView1", Model);
   else
       Html.RenderPartial("PartialView2", Model); %>

This will render either PartialView1 or PartialView2 using the same Model depending on the condition set by the Controller.

Or, to return a Partial View with a Model directly from your controller, instead of a normal View, you can do this:

public ActionResult MyActionMethod()
{
    var model = ...
    ViewData["MyViewData"] = ...
    return PartialView("PartialView1", model);
}

This will return the Partial View PartialView1 directly to the client. This is mostly useful in AJAX scenarios, since the result will most probably not be an entire HTML page. Partial Views are .ascx files.

bzlm
Ideally i need to control the rendered PartielView through the Controller. How can that be done?
shahid81
@shahid81 Why do you need to do this? What's your scenario? You can still control which Partial View is rendered *through the Controller* using the method I described, as long as the Controller is the one to determine whether `condition` is met.
bzlm
I have a small form with a number of outcomes, different confirmation screens but i dnt want to have different URLs. Like webforms use to do with Multiviews.
shahid81
@shahid81 The URL is connected to the Action Method, so you can still have the same URL for any outcome. If the Controller knows the outcome, and hence which Partial View to render, why not use my method? Or return a Partial View directly from the Controller?
bzlm
@bzlm How would I return a partial view directly from my Controller? Code example?
shahid81
@shahid81 I've updated my answer. But I don't think you can be helped without more information about *what* you want to accomplish.
bzlm
thanks @bzlm. Basically, what i want accomplish is dynamically inserting a PartialView into a View via an action method. I dont mind using your method but i dnt understand how to control your View code with the 'if' statement from the Controller
shahid81
@shahid81 I've updated my answer again. Is it more clear now?
bzlm
While this is possible with some creepy magic like view serialization, it's wrong to do that and You should reread this answer. Do not abuse tools, You are shooting in Your foot.
Arnis L.
@bzlm you are a Legend!
shahid81