问题
I'm new to working with MVC so please don't assume I know anything.
I am picking up a project that has much already written in MVC and I am trying to add some things to it.
On one View there is a line
<% Html.RenderAction("List", "Image", new { id = Model.JobId, all = true }); %>
I see a List.ascx under the Image directory. I see the List method on the view controller.
I'd like to render the results of that list method to a different ascx file. (AssignImage.ascx) I realize I could add another method on the controller, but it seems like I should have a way of using the same method but a different view.
回答1:
If you don't mind reusing (or duplicating) some code I'd probably just make a new action to deal with this.
I don't think I would change the action to pass in another parameter (the action already is taking 2: a jobId and a boolean). You'd probably have to change existing code somewhere to account for a third parameter.
Assuming the action is just giving you a list of records I don't see how adding a new action with one line of LINQ (or however you are getting data) would offend the DRY... especially if it makes the code easier to maintain by not mixing too many functions in one action. If its too offensive, then you can refactor the actions to call some common method.
回答2:
In your Action method
if (isList) return PartialView("List"); else return PartialView("AssignImage");
来源:https://stackoverflow.com/questions/1948230/how-do-i-render-an-alternate-child-view-in-mvc