0

I could not find the similar question yet, so I decided to ask it here.

I relatively new to MVC and may have some incorrect wording in my question, and I'm just wondering if that is possible to do it at all?

Usually, we are dealing with ModelViewController coupling and we return the View from a Controller with Models/Json as parameters to the returning View, so we can bind the Model to the View

I'm just wondering if we have a ViewA, ViewB ControllerA,ControllerB and a ModelA, is that possible to have a @Url.Action/Link or Ajax method to send the ModelA from the ViewA to an Action Method of a ControllerB, so, the data stored in the ModelA can be displayed in the ViewB when it is returned from the ControllerB?

I do not have any code yet and just want to know if that is possible and if so, what would be the right approach to achieve something like that?

ethan codes
  • 87
  • 10
gene
  • 2,098
  • 7
  • 40
  • 98
  • Just trying to make sure I understand the question correctly: You have data from Model A in View A, now you want to take that data and pass it into Controller B, then pass that data into another ViewB? Is that correct? – ethan codes Mar 06 '18 at 15:10
  • Yes, something like that. Is that possible? – gene Mar 06 '18 at 15:12
  • Do you need to actually do any work with the data, or are you just trying to take it from one view to another? – ethan codes Mar 06 '18 at 15:16
  • I will not do any work with the data, I will just need to be able to show that data and refresh it based on the DatePicker and DropDown values changed coming from another partial view. That functionality is already working though – gene Mar 06 '18 at 15:29

1 Answers1

0

You could do something like this: Controller B:

    public IActionResult ControllerB(ModelA data)
    {
        return View(data);
    }

View A:

@foreach (var data in Model)
{
    <li>@Html.ActionLink(@data.ModelAProperty, "ControllerB", "ControllerBFileName", new { id = data.Sys.Id })</li>
}

View B:

@model YourModel.Models.ModelA
<div>

<h3>@Model.ModelAProperty</h3>

<p>@Model.ModelAOtherProperty</p>
</div>

This should work I believe. I did this with a previous project but it was a slightly different set up, but I believe I have modified it correctly to fit your needs. Basically you are passing the data to the controller with the first view, and then using that controller to pass the data to the next view.

ethan codes
  • 87
  • 10