简体   繁体   中英

C# MVC, controller action not being called on view render

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM