How can I better handle this situation in ASP.NET MVC regarding my Partial Views?

I have one view in which there is a menu on the left and a container on the right side that is displayed from one of several partial views.

Right now I am facing a situation similar to the following index view:

<div class="container">  
    <div class="leftHandMenu">  
        // various dynamic menu items  
    </div>  
    <div class="rightHandPane">  
        <% if (Model.CurrentPane == "Sent") { %>  
            <% Html.RenderPartialView("SentPartial", Model.SomeData);  %>  
        <% } else if (Model.CurrentPane == "Inbox") { %>   
            <% Html.RenderPartialView("InboxPartial", Model.SomeData);  %>  
        <% } else if (Model.CurrentPane == "Alerts") { %>  
            <% Html.RenderPartialView("AlertsPartial", Model.SomeData);  %>  
        <% } %>  
    </div>  
       // various other common view items  
</div> 

with the following actions:

public ActionResult Inbox(int? page)  
{  
    MessageListViewModel viewData = new MessageListViewModel();
    viewData.SomeData = messageService.getInboxMessages(page.HasValue ? page.Value : 0);
    viewData.CurrentPane = "Inbox";
    return View("Index", viewData);
}  

public ActionResult Alerts(int? page)  
{  
   MessageListViewModel viewData = new MessageListViewModel();
    viewData.SomeData = messageService.getAlertMessages(page.HasValue ? page.Value : 0);
    viewData.CurrentPane = "Alerts";
    return View("Index", viewData);
} 

public ActionResult Sent(int? page)  
{  
    MessageListViewModel viewData = new MessageListViewModel();
    viewData.SomeData = messageService.getSentMessages(page.HasValue ? page.Value : 0);
    viewData.CurrentPane = "Sent";
    return View("Index", viewData);
}  

I understand that this situation is not perfect, but I need these parts in order to remain partial. I make a bunch of ajax calls in this view to reload this “right pane” with various partial views.

"" , , "" . ViewModel, if-else .

? 3 , . , ViewModel.

-, , ? Master View , , Master View, ASP.NET MVC.

!

+3
4

, ?

.

<div class="container">  
    <div class="leftHandMenu">  
        <asp:ContentPlaceHolder ID="LeftHandMenuContainer" runat="server" />
    </div>  
    <div class="rightHandPane">  
         <asp:ContentPlaceHolder ID="RightHandPaneContainer" runat="server" />
    </div>  
      <asp:ContentPlaceHolder ID="FooterContainer" runat="server" />  
</div>

.

+2

:

Html.RenderPartialView(string.Format("{0}Partial", Model.CurrentPane), Model.SomeData);  %> 

Partial View. $.02.

+1

a) ( ASP.NET 4), Html.Action() Html.RenderAction(), , .

b) if- . :

public static void RenderRightHandPane(this HtmlHelper helper, YourViewModelType model)
{
    switch(model.CurrentPane.ToLower())
    {
        case "sent":
            helper.RenderPartialView("SentPartial", model.SomeData);
        case "inbox":
            helper.RenderPartialView("InboxPartial", model.SomeData);
        case "alerts": 
            helper.RenderPartialView("AlertsPartial", Model.SomeData);
        case default:
            // Add a default handler - perhaps rendering a not found-view...?
    }
}

:

<% Html.RenderRightHandPane(Model); %>
0

You can “code by convention” and follow this pattern:

<div class="rightHandPane">  
    <% Html.RenderPartialView(Model.CurrentPane + "Partial", Model.SomeData);  %>  
</div>  
0
source

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


All Articles