0

I'm moving my WebForms project to MVC and having a hard time designing things. The basic display of my app is in _Layout. The page is divided into 4 parts(say Part A,B,C and D), with 3(A,B,C) just containing html and one(D) is dynamic. I had used @RenderBody to bring in the content of Part D. However, now the other parts are changing and I need separate controllers for these parts. What is the best way to get their contents to be displayed into _Layout?

@Html.RenderPartial / @Html.Partial / @Html.RenderAction / @Html.Action ?

I'm currently trying to replace Part C by using - @Html.Action("Index", "CController")

However, this is not working.

In Index.cshtml for CController, I've the Layout = null, initially it was set to point to _Layout.cshtml, but I read here that this created issues.

After putting the C Part in CControllers view, it does not event display the basic _Layout page that it displayed earlier.

Here's the Index.cshtml of CController -

<div id="noteContainerDiv">
    Here goes all the data to display
</div>

And Here's the code for CController.cs -

public class CController : Controller
{        
    public ActionResult Index()
    {
        return PartialView();
    }

}

Can anyone suggest the right way to design this?

Community
  • 1
  • 1
neuDev33
  • 1,573
  • 7
  • 41
  • 54

2 Answers2

0

What you could do is have a view model for your subview (the one you call D) and pass in this view model from your layout view.

I.e. by doing something like this in your _Layout:

@Html.Partial("_SubviewD", Model.SubviewDModel)

Then obviously in your controllers you need to initialize this subview model and include it in your model (or alternatively, in the ViewBag--apologies for suggested being evil!) You could even find ways to do this without changing all your controllers (for example through a base Controller class and overriding OnActionExecuted).

Clafou
  • 15,250
  • 7
  • 58
  • 89
0

There are many ways you could do this. But I would stick to your initial approach.

Have your CController Index action return a PartialViewResult instead of a full result.

You don't set the layout for partial views. So in your CController index action you'll have something like this:

var model = ....
return this.PartialView("NameOfYourPartialView", yourModel);

And when you call

@Html.Action("Index", "CController")

Everything should be OK, cool?

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • I understand from your comment what I was doing wrong. I've modified C'c controller to return a Partial View for say the Index action and currently have static html sitting in Index.cshtml. However, just including @Html.Action("Index", "C") isn't working either. – neuDev33 Jun 15 '12 at 15:19
  • Place a breakpoint inside your action that should be printing the partial view just to make sure it's the one executing. Then make sure that your partialview is OK and has something to return. Can you paste it here? – Fabio Milheiro Jun 15 '12 at 15:51
  • Sure, I've edited my question and added the code there. Currently, I'm just trying to display some static html. – neuDev33 Jun 15 '12 at 17:29
  • In order to render only static content, you may use @{ Html.RenderPartial("_FeaturedProduct"); } – Fabio Milheiro Jun 15 '12 at 23:50
  • But did you manage to place the breakpoint in the action to make sure that it was being hit (it should, but just to make sure). Also, are you using Action or RenderAction method? The RenderAction method should be used like this: @{ this.Html.RenderAction(...); } while Action method should be: @this.Html.Action(...) (because RenderAction writes to the response and Action just returns a string which is printed to the page with the @ sign. – Fabio Milheiro Jun 15 '12 at 23:52