0

Please advice the best way to organize editing my Restaurant entity in MVC3.

I already have _Layout page which has header and footer common for all the pages of the site. _Layout has Section with "Content" as a name.

For restaurant editing I've create another layout called _Restaurant which adds side navigation with options to edit basic information, photo, categories, etc. All navigation links are created with command like this @Html.ActionLink("Some name","Edit", "Restaurant", new {id=Model.ID}), all links have the same contoller name and routing values. Model for this layout is Restaurant (it is needed to construct proper links for navigation)

At the moment I've implemented Views for Edit and Photo and this structure works quite good, because both views use Restaurant as Model. But I am afraid that I might have some problems when trying to implement editing of restaurant products and categories, because this pages will require different Models.

What would you advice in this situation? I know that I can use partial view to generate subnavigaton, but in this case I would need to insert the rendering code in all the views I generate, so I think it is not the best solution.

  • For the sub-navigation I would first ask the question of whether the sub-navigation is static (i.e. the same throughout the site for every page), whether there is a static sub-navigation for each main navigation item or whether it is generated dynamically from contents in the database perhaps? – Dangerous May 20 '12 at 16:08
  • All Restaurants have the same navigation links (General Info, Photo, Categories, etc), but for each of them I need to create different Urls (i.e. mydomain.com/restaurant/edit/7/ or mydomain.com/restaurant/photo/8/) – Alexander Romanov May 20 '12 at 18:38
  • I have updated my answer with further information on how to render the navigation urls. Hope this helps. – Dangerous May 20 '12 at 21:56

1 Answers1

1

It sounds like the Restaurant model that you have is part of your domain model and you are using it in your views. If so then you should consider using view models where each model is specific to the view that you are creating. The contents of the view model can be mapped to/from the domain model(s) as required to prevent you getting the mismatch of fields that you mentioned with implementing you product edit and category edit views. This mapping can be performed manually (i.e. copying field by field) or you can use a tool such as automapper to perform this mapping for you.

Edit:

From my understanding if you visit the restaurant part of your site you have a side navigation where the links are always the same but need to be generated dynamically for that restaurant.

Does the _Restaurant layout comtain similar elements as those defined in the main _Layout? If so, I would make the _Restaurant layout a nested layout (if it isn't already) so you don't have to redefine those elements.

For example:

The main _Layout would be defined:

<html>
...
<body>
    <!--main navigation here-->
    @RenderBody()
<body>
</html>

Then you nested _Restaurant layout could be defined:

@{
    Layout = "_Layout.cshtml"
}
<!-- Define elements for the restaurant layout here-->
@RenderBody()

You can define your restaurant elements in this layout but you can't define your side navigation here as it needs the view model to be generated. Therefore, if the only thing to appear in the nested layout is the side navigation then I wouldn't even use a nested layout. Otherwise, (if other elements are defined in the nested layout), then you could define a section (@RenderSection) in the nested layout to ensure all the views that use it define a side navigation.

Then each view would then be defined similar to the following (using a child action to render the side navigation):

@model ...

@{
    Layout = "~/_Restaurant.cshtml"
}

@Html.RenderAction("SideNavigation", "RestaurantController", new { id = Model.Id })

The child action would then be defined as:

public class RestaurantController : Controller
{
    [ChildActionOnly]
    public PartialViewResult SideNavigation(int id)
    {
        return PartialView();
    }
}

This would then pass the id to the partial view which would be defined as:

@model int

<!--code to display the side navigation-->

Hopefully this should help.

Community
  • 1
  • 1
Dangerous
  • 4,818
  • 3
  • 33
  • 48
  • What mapping tool would you suggest to use? I use NHibernate to work with domain model classes and I tried to use AutoMapper to do mapping between View and Domain models, but for some reasons it failed to validate mapping configuration with message like "cannot find ISession". – Alexander Romanov May 20 '12 at 18:41
  • I have only used AutoMapper. I'm not sure why your getting the error message your getting. If you needed more help with this you would have to post another question with more information in order for other users to advise as its not directly related to this question. – Dangerous May 20 '12 at 21:55