【发布时间】:2019-06-27 01:51:16
【问题描述】:
概述:
我实际上是在构建一个单页应用程序。三个输入和两个基于输入数据加载的表。
每个表都设置为一个选项卡,每个选项卡都有一个按钮来加载各自表的数据。一个表设置在局部视图中。我正在尝试看看它是否有效,以便我可以将两个表都设置为部分视图。
问题: 当我单击提交按钮时,部分视图正在将表格加载到新窗口中。
示例: 因此,在加载 Web 应用程序时,例如,“http://localhost:30000/CommissionDatas/”索引页面加载页面和空表就可以了。
我正在使用 ViewModel,因为每个表都使用不同的模型,并且我会收到有关具有不同数据模型的 Partial 视图表的错误。
一旦我点击按钮“gpmbutton”,对于局部视图表,按钮使用'formmethod'属性调用actionresult方法'_TrueUp',它将检索数据并将数据模型返回到局部视图。但是,部分视图的表格及其数据最终会发布到“http://localhost:30000/CommissionDatas/_TrueUp”,这是一个全新的页面。
我已经尝试将 actionresult 方法类型更改为 PartialViewResult 并将返回类型从“PartialView()”更改为控制器中的“视图”,但仍然无效。我还尝试在索引页面中使用@Partial 以及在局部视图中使用@RenderPartial,我得到了相同的结果。
此外,“Index”和“_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);
}
【问题讨论】:
-
我尽力了解这里发生了什么以及真正的问题是什么。但我想不通。尝试从 @Url.Action([action], [controller]) 代码构建您的 FormAction 属性,以获得正确的相对 url 到正确的操作方法。
-
是我的描述让你失望了吗?
标签: c# razor asp.net-mvc-5 viewmodel partial-views