MVC 5 PartialVIew正在不同的页面中加载



概述:

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

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

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

示例:因此,例如,在加载web应用程序时http://localhost:30000/CommissionDatas/'索引页面加载页面和空表很好。

我使用ViewModel是因为每个表都使用不同的模型,并且我会得到一个关于Partial视图表具有不同数据模型的错误。

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

我已经尝试在控制器中将actionresult方法类型更改为PartialViewResult,并将返回类型从"PartialView()"更改为"View",但仍然没有成功。我还尝试在索引页中使用@Partial,在局部视图中使用@RenderPartial,得到了相同的结果。

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

请帮忙!

附言:我删除了对问题不重要的代码,因为数据是敏感的。

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);
}

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

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

编辑:

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

最新更新