0

I am trying to create a single page application in C# MVC 4.5.

On my application I am going to have 1 main view, which calls 3 separate views in it. All the three views work independently off of each other and refresh by themselves.

For some reason my controller methods for those 3 views are not being called when the views are rendered. Any ideas why?

Controller:

public ActionResult Index()
{
    if (!_instance.Users.CheckUserSkillsExist(WebSecurity.CurrentUserName))
    {
        return RedirectToAction("CreateChar");
    }

    _instance.GameBase.GetBaseData();

    return View();
}

public ActionResult PlayerDisplayStats()
{
    var user = _instance.Users.GetUserSkills( WebSecurity.CurrentUserName);
    var userModel = new UserModel(user);
    return View(userModel);
}

View: Index.cshtml

<div class="span2 offset1">
        <div class="span12">
            @RenderPage("~/Views/Game/PlayerDisplayStats.cshtml");
        </div>
        <div class="span2">
            @RenderPage("~/Views/Game/GameNavigator.cshtml");        
        </div>
    </div>
    <div class="span8">
        @RenderPage("~/Views/Game/MainWindow.cshtml");        
   </div>

View: PlayerDisplayStats.cshtml

<div class="row-fluid tileOpaque">
    <div class="span12 content">
        <div class="img avatar">               
        </div>
        <div class="span12">
            <div class="span6 stat">
                @Html.LabelFor(m => m.StrVal)
                @Html.DisplayFor(m => m.StrVal)
            </div>
            <div class="span6 stat">
                Det:
            </div>
        </div>
    </div>
</div>
Marc
  • 3,739
  • 4
  • 19
  • 34
ArjaaAine
  • 826
  • 5
  • 13
  • 27

1 Answers1

2

I think it's likely that your problem is due to using @RenderPage instead of @Html.Action. Using @Html.Action means your view will go via the controller, and therefore get the necessary model when doing so. @RenderPage is simply including the view in the response. I encourage you to read this SO entry for the differences:

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

What you'll (probably) end up with is something like this...

<div class="span2 offset1">
    <div class="span12">
        @Html.Action("PlayerDisplayStats")
    </div>
    <div class="span2">
        @Html.Action("GameNavigator")
    </div>
</div>
<div class="span8">
    @Html.Action("MainWindow")
</div>

You will need to alter your controller to return a PartialView also...

public ActionResult PlayerDisplayStats()
{
    var user = _instance.Users.GetUserSkills( WebSecurity.CurrentUserName);
    var userModel = new UserModel(user);
    return PartialView(userModel);
}

Later, using jQuery you could then fetch the individual partial views and inject the updated view into the page.

Community
  • 1
  • 1
Tentux
  • 704
  • 1
  • 4
  • 13
  • So, action/RenderAction work.. but it seems like when I use Html.Action it doesn't load the new view inside my previous view.. but rather independent of it. Like: I am calling the view PlayerDisplaystats in a div which is span2 wide.. But for some reason it loads the view in a span12 div. – ArjaaAine Sep 15 '13 at 19:13
  • Check you're returning a PartialView. What you should end up with is
    where the span12 class is part of Index view and row-fluid view is part of the PlayerDisplayStats view. Post your output HTML if it will be helpful.
    – Tentux Sep 16 '13 at 12:09
  • Also it seems your class in the PlayerDisplayStats view is '
    ' - is this not the css class you wanted?
    – Tentux Sep 16 '13 at 12:12
  • I want it to be span12 inside a span2 div. So what it means by twitter bootstrap methodology is that it will take the entire space (span 12) of its parent div (span2) But after trying to get the html for you, i found the answer. With renderpartial, it wasn't generating the layout for the subview. With RenderAction it was adding the layout's code on my subview, hence breaking it. So, i overrode the layout in the subview and it works correctly now. THANKS a lot! – ArjaaAine Sep 16 '13 at 22:24