gene
gene

Reputation: 2108

Is it possible to send Model data from a View to a Controller that is not the Controller for that View?

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?

Upvotes: 0

Views: 21

Answers (1)

ethan codes
ethan codes

Reputation: 87

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.

Upvotes: 0

Related Questions