简体   繁体   中英

Passing view from one controller to another controller's view

Is it possible to pass another controller's view to the first controller's view? I have controller1 with view1. I need to call another controller2 action method from view1 and pass the view2 to a div in view 1.

I tried @html.Action("action","controller") . This called controller 2, but was not passing the view2 to view1.

Am I doing it wrong? How can I do that?

This example is something you can use. I did not put it into an ASP.NET Fiddle because we are dealing with TWO view.

Controller/ViewModel of First

namespace Testy20161006.Controllers
{
    //I'm showing how to pass data from one Controller Action to another Controller Action.
    //With the data you can render your second view however you like with the data.
    //We pass data NOT views.  You could use a partial view, but I am showing the most basic way.
    public class NewbieDevViewModel
    {
        public String DataToPassToNewControllerAction { get; set; }
    }

    public class HomeController : Controller
    {
        //I am using Tut145 for my first Controller/Action/View, but you could have called it Index
        [HttpPost]
        public ActionResult Tut145(NewbieDevViewModel passedData)
        {
            //passing simple string, so I can pass it using my QueryString
            return RedirectToAction("MyAction2", "Home2", new { passedData = passedData.DataToPassToNewControllerAction });
        }

        public ActionResult Tut145()
        {
            return View();
        }

View of First

@model Testy20161006.Controllers.NewbieDevViewModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut145 - View 1</title>
   </head>
<body>
    @using (Html.BeginForm())
    {
        @Html.LabelFor(r=>r.DataToPassToNewControllerAction)
        @Html.TextBoxFor(r => r.DataToPassToNewControllerAction, new { @Value = "ValueOfData" })
        <input type="submit" value="Submit data - to send to new Controller Action" />
    }
</body>
</html>

Controller of Second

namespace Testy20161006.Controllers
{
    public class Home2Controller : Controller
    {
        //I named my Controller Home2 and Action MyAction2, but you can name it anything you want
        public ActionResult MyAction2(string passedData)
        {
            //reconstruct the ViewModel and pass into second view
            NewbieDevViewModel viewModel = new NewbieDevViewModel { DataToPassToNewControllerAction = passedData };
            return View(viewModel);
        } 

View of Second

@model Testy20161006.Controllers.NewbieDevViewModel
@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>MyAction2 </title>
</head>
<body>
    <div>
        - Final View - I passed data into here from different Controller Action -and-
        I can render this page anyway I which
    </div>
    <p/>
    @Html.LabelFor(r => r.DataToPassToNewControllerAction)
    @Html.TextBoxFor(r => r.DataToPassToNewControllerAction)
</body>
</html>

Partial view can be used to render a view inside another view. Create a partial view for an action in controller 2. Call that partial view from the view of controller 1. Here is the example :

First Controller :

public class Controller1Controller : Controller
{
    public ActionResult Edit()
    {
        return View();
    }
 }

First Controller View :

@using (Html.BeginForm())

{ @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Controller 1 View</h4>
    <hr />
    <h1>Fisrt Controller</h1>
    <div>
        @{
            Html.RenderAction("GetSubject", "Controller2");
        }
    </div>               
</div>

}

Second Controller :

 public class Controller2Controller : Controller
 {       
    public ActionResult GetSubject()
    {
        Subject s = new Subject() { id = 2, SubjectName = "XYZ" };
        return PartialView(s);
    }        
 }

Second Controller View :

<div>
    <h4>Controller 2 view</h4>
<hr />
    <h1>Second Controller</h1>
</div>

After spending some time on the code and a bit of googling, I figured out my issue. The child action method I was calling from parent view was an async method, so I did something like the below,

Parent view

<div id="childView"></div>

Ajax Call to populate the parent view

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url : '@Url.Action(actionName: "ChildAction", controllerName: "ChildController")',
        dataType: "html",
        async:true,
        success: function (result) { $("#childView").html(result); }
        });

 });

Hope it will be useful for some one.

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