How to render an alternate child view in MVC?

I am new to MVC, so please do not assume that I know something.

I am building a project that has already been written a lot in MVC, and I'm trying to add some things to it.

One view has a line

<% Html.RenderAction("List", "Image", new { id = Model.JobId, all = true }); %> 

I see List.ascx in the Image directory. I see the List method on the view controller.

I would like to make the results of this list method another ascx file. (AssignImage.ascx) I understand that I could add another method on the controller, but it looks like I should have a way to use the same method, but a different view.

+1
source share
2 answers

If you don't mind reusing (or duplicating) the code, I would probably just take a new action to handle this.

I do not think that I would change the action to pass another parameter (the action already takes 2: jobId and boolean). You may have to modify the existing code somewhere to account for the third parameter.

Assuming the action just gives you a list of entries, I don’t see how adding a new action with a single LINQ line (or, nevertheless, you get the data) will offend DRY ... especially if it makes the code easier to maintain without mixing too much many functions in one action. If this is too offensive, you can reorganize the actions to call some kind of general method.

+1
source

In the action method

if (isList) return PartialView("List"); else return PartialView("AssignImage");

+2
source

Source: https://habr.com/ru/post/1397808/


All Articles