简体   繁体   中英

MVC 5 PartialVIew is loading in Different Page

Overview:

I am essentially building a single page application. Three inputs and two tables that load based on the inputs' data.

Each table is set to a tab and each tab has a button to load their respective table's data. One table is set in a partial view. I'm trying this out to see if it works so I can set both of the tables to a partial view.

Problem: The partialview is loading the table into a new window when I click the submit button.

Example: So, upon loading the web application, for example, ' http://localhost:30000/CommissionDatas/ ' the Index page loads the page and the empty tables just fine.

I am using a ViewModel because each table uses a different model and I will get an error about the Partial view table having a different data model.

Once I click the button "gpmbutton", for the partial view table, the buttion uses the 'formmethod' attribute to call actionresult method '_TrueUp' and it will retrieve the data and return the data model to the partial view. But, the partial view's table and it's data ends up posting the to ' http://localhost:30000/CommissionDatas/_TrueUp ', which is a completely new page.

I have already tried changing the actionresult method type to PartialViewResult and changing the return type from 'PartialView()' to a 'View' in the controller and that still did not work. I've also tried using @Partial in the index page as well as @RenderPartial for the partial view and I get the same result.

Also, both the 'Index' and the '_TrueUp' PartialView page are in the same folder under 'CommissionDatas' in the views folder.

Please HELP!

PS I removed the code that is not essential to the problem as the data is sensitive.

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>

This is the Partial View

_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>
    }

This is the Controller.

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

 }

Please try @Html.Action("yourAction", "yourController") instead of @Html.RenderPartial("_TrueUp").

For further information visit: How can I use Html.Action? MSDN

EDIT:

Use: @Html.Partial("partialViewName", partialViewModel) partialViewModel is optional.
In your case: @Html.Partial("_TrueUp").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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