question

SantoshUmarani-1390 avatar image
0 Votes"
SantoshUmarani-1390 asked YijingSun-MSFT answered

Reusing the same web page in MVC

Hi,

I have a web page which is being developed in MVC. I wanted to reuse the same implementation first and then modify/add some minor things.
Should I have a copy of all the implementation of model, view and controller ? or Is there any other better way ?
Can anyone please let me know what is the best way to handle such scenario? (I hope I am not asking some obvious question)
Kindly waiting for your response.

Thanks,
Santosh

dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @SantoshUmarani-1390 ,
If you want to re-use presentation logic only, you can use partial view. If you want to re-use also controller's logic, you have to use child action combined with partial view.

Create a controller:

 public class IssuesController : Controller
 {
     [ChildActionOnly]
     public PartialViewResult List(string projectName, int issueCount = 0)
     {
         IEnumerable<Issue> issueList = new List<Issue>();
    
         // Here load appropriate issues into issueList
    
         return PartialView(issueList);
     }
 }

Do not forget also to create appropriate partial view named List within the folder Issues.

Finally use this line within your project view

 @{ Html.RenderAction("List", "Issues", new { projectName = "Px", issueCount = 10 } ); }

and this line within your issue list view

 @{ Html.RenderAction("List", "Issues", new { projectName = "Px" } ); }

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.