首页 > 解决方案 > MVC 5 PartialVIew 正在加载不同的页面

问题描述

概述:

我本质上是在构建一个单页应用程序。三个输入和两个基于输入数据加载的表。

每个表都设置为一个选项卡,每个选项卡都有一个按钮来加载各自表的数据。一个表设置在局部视图中。我正在尝试这个看看它是否有效,所以我可以将两个表都设置为部分视图。

问题:当我单击提交按钮时,部分视图正在将表格加载到新窗口中。

示例:因此,在加载 Web 应用程序时,例如,“ http://localhost:30000/CommissionDatas/ ”索引页面加载页面和空表就可以了。

我正在使用 ViewModel,因为每个表都使用不同的模型,并且我会收到有关具有不同数据模型的部分视图表的错误。

一旦我单击按钮“gpmbutton”,对于局部视图表,该按钮使用“formmethod”属性调用 actionresult 方法“_TrueUp”,它将检索数据并将数据模型返回到局部视图。但是,部分视图的表和它的数据最终会发布到“ http://localhost:30000/CommissionDatas/_TrueUp ”,这是一个全新的页面。

我已经尝试将 actionresult 方法类型更改为 PartialViewResult 并将返回类型从“PartialView()”更改为控制器中的“视图”,但这仍然不起作用。我还尝试在索引页面中使用@Partial 以及在局部视图中使用@RenderPartial,我得到了相同的结果。

此外,“Index”和“_TrueUp”PartialView 页面都位于视图文件夹中“CommissionDatas”下的同一文件夹中。

请帮忙!

PS我删除了对问题不重要的代码,因为数据很敏感。

Index.cshtml
-------------------------------------------------------------
@model CommissionReport.Models.ViewModels.CommissionViewModel
@{
   Layout = null;
 }

<!DOCTYPE html>

<html>
<head>
</head>
<body>

    <input type="submit" ID="commissionbutton" 
      formaction="ReturnCommissionData" 
      formmethod="post" 
    />   

    @if (Model != null)
    {
    <table id="mainTable" class="ui striped selectable table">
      <thead>
        <tr>
        </tr>
      </thead>
      <tbody>
       @foreach (var item in Model.CommissionData)
         {
            <tr>
            </tr>
         }
      </tbody>

    </table>
    }

    <input type="submit" class="btn btn-primary" ID="gpmbutton" 
      formaction="_TrueUp" formmethod="post" 
    />

    <div>
       @if (Model != null)
       {
          Html.RenderPartial("_TrueUp");
       }
    </div>

</body>
</html>

这是部分视图

_TrueUp.cshtml
----------------------------------------------------------------------------
@model CommissionReport.Models.ViewModels.CommissionViewModel

@{
    Layout = null;
    var trueupmodel = Model.TrueUp;
}

    @if (Model != null)
    {
    <table id="mainTable" class="ui striped selectable table">
      <thead>
        <tr>
        </tr>
      </thead>
      <tbody>
       @foreach (var item in Model.TrueUp)
         {
            <tr>
            </tr>
         }
      </tbody>

    </table>
    }

这是控制器。

private CommissionViewModel vm = new CommissionViewModel();

[HttpPost]
public ActionResult ReturnCommissionData(FormReturn form)
{
        //Code to return data here

        vm.CommissionData = db.CommissionDatas.ToList();
        return View("Index", vm);

}

<HttpPost>
public ActionResult _TrueUp(FormReturn form)
{

    //Code For Data to be returned here
    vm.TrueUp = model;

    return PartialView("_TrueUp", vm);

 }

标签: c#razorasp.net-mvc-5viewmodelpartial-views

解决方案


请尝试@Html.Action("yourAction", "yourController") 而不是@Html.RenderPartial("_TrueUp")。

如需更多信息,请访问: 如何使用 Html.Action? MSDN

编辑:

使用:@Html.Partial("partialViewName", partialViewModel) partialViewModel 是可选的。
在您的情况下:@Html.Partial("_TrueUp")。


推荐阅读