Neil
Neil

Reputation: 2749

Nested Lists in MVC 4 page

I have an object relationship as follows:

Refund contains a list of Vouchers

Voucher contains a list of line items

I want to make a single MVC page that can allow a user to enter in all the details of a refund, including populating the vouchers and line items. So there would be a form for vouchers with an add button to add them to the current refund, and a form for line items with a button to add them to the current voucher

Initially I set out to make a Refund controller which would handle the refund, and it's views would contain a partial view for entering the voucher data. This view would be handled by the voucher controller, and the voucher controller would be responsible for gathering the information about each voucher.

On the View of the voucher controller there would be a partial view for entering the line items. this would be handled by the line items controller.

Now I'm quite new to MVC and maybe haven't got my head fully around it, but I'm getting a bit worried that this isn't the correct way to do things. For example when the user enters in all the line item details and submits to an Ajax form, I assumed it would be the line items controller that handles this. If I do this though, my refund object has no idea of any of this, and the end goal is to populate the refund object on the refund controller.

Am I suppose to pass data from one controller to another, or call an action from one controller from a view of another, or should my add button be on the voucher controller, or on the refund controller? Any pointers are greatly appreciated

Upvotes: 0

Views: 220

Answers (1)

Jack
Jack

Reputation: 1352

From within any view, you can call an action from any controller. As an example, the Razor ActionLink helper allows you to pass a controller name along with the method name from that controller:

@Html.ActionLink("Link text, "Action Name", "Controller Name")

And a form that posts to a different controller than the one that rendered the view:

@Html.BeginForm("Action Name", "Controller Name", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    // Form fields
}

Whether or not you want to do this depends on the overall design of your application. It may be simpler to create a "RefundVoucher" ViewModel that encapsulates the data that you need from your Refund and Voucher Models and create a controller and strongly-typed views for that.

Upvotes: 2

Related Questions