簡體   English   中英

MVC 5 PartialVIew正在不同的頁面中加載

[英]MVC 5 PartialVIew is loading in Different Page

概述:

我實質上是在構建一個單頁應用程序。 三個輸入和兩個基於輸入數據加載的表。

每個表都設置為一個選項卡,每個選項卡都有一個按鈕以加載各自表的數據。 在局部視圖中設置了一個表。 我正在嘗試這樣做,看看是否可行,因此我可以將兩個表都設置為局部視圖。

問題:當我單擊提交按鈕時,partialview正在將表加載到新窗口中。

示例:因此,在加載Web應用程序(例如,“ http:// localhost:30000 / CommissionDatas / ”)后,索引頁面將加載該頁面,而空白表就可以了。

我使用的是ViewModel,因為每個表使用不同的模型,並且我會收到有關具有不同數據模型的Partial視圖表的錯誤。

單擊部分視圖表的按鈕“ gpmbutton”后,按鈕將使用“ formmethod”屬性調用actionresult方法“ _TrueUp”,它將檢索數據並將數據模型返回到部分視圖。 但是,部分視圖的表及其數據最終發布到“ http:// localhost:30000 / CommissionDatas / _TrueUp ”,這是一個全新的頁面。

我已經嘗試將actionresult方法類型更改為PartialViewResult並將返回類型從“ PartialView()”更改為控制器中的“視圖”,但仍然無法正常工作。 我也嘗試過在索引頁面中使用@Partial以及對部分視圖使用@RenderPartial,並且得到相同的結果。

同樣,“索引”和“ _TrueUp” PartialView頁面都位於views文件夾中“ 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);

 }

請嘗試@ Html.Action(“ yourAction”,“ yourController”)而不是@ Html.RenderPartial(“ _ TrueUp”)。

有關更多信息,請訪問: 如何使用Html.Action? MSDN

編輯:

使用:@ Html.Partial(“ partialViewName”,partialViewModel)partialViewModel是可選的。
在您的情況下:@ Html.Partial(“ _ TrueUp”)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM