简体   繁体   English

C#MVC,未在视图渲染上调用控制器操作

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

I am trying to create a single page application in C# MVC 4.5. 我试图在C#MVC 4.5中创建单页面应用程序。

On my application I am going to have 1 main view, which calls 3 separate views in it. 在我的应用程序中,我将有一个主视图,它在其中调用3个单独的视图。 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. 出于某种原因,在渲染视图时,不会调用这3个视图的控制器方法。 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 查看: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 查看: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. 我认为你的问题很可能是由于使用了@RenderPage而不是@Html.Action。 Using @Html.Action means your view will go via the controller, and therefore get the necessary model when doing so. 使用@ Html.Action意味着您的视图将通过控制器进行,因此在执行此操作时会获得必要的模型。 @RenderPage is simply including the view in the response. @RenderPage只是在响应中包含视图。 I encourage you to read this SO entry for the differences: 我鼓励您阅读此SO条目以了解差异:

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction 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... 您需要更改控制器以返回PartialView ...

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. 稍后,使用jQuery,您可以获取单独的部分视图并将更新的视图注入页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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