views:

44

answers:

1

I'm currently working on a portal using jquery portlets/sortable/draggable that also includes a content management system, all in MVC2. Only administrators are able to change the layout/content of the site currently.

Each view gets the personalization for a page (from the base controller) based on Controller and Action. Then the view loops through the widgets and calls renderaction for each of them.

Currently, I have View + "Edit" actions on each view to set the page into edit mode, as I'm duplicating code there must be a better way but I can't see it for the life of me!

How would you implement an action that allows each View to be edited?

 public ActionResult Legal()
        {
            PageModel model = GetPageSetting();
            return View("Portal", model.PageSetting.Master.Path, model);
        }

    [HttpPost]
    [Authorize(Roles = "Administrator")]
    public ActionResult LegalEdit(EditorModel e)
    {
        PageModel model = GetPageSetting("Legal", "Home", true);
        return View("Portal", model.PageSetting.Master.Path, model);
    }

//This is in the base controller

    protected PageModel GetPageSetting(string action, string controller, bool isEditing)
    {
        PersonalizationProcess personalizationProcess = new PersonalizationProcess();

        string path = string.Format("~/{0}/{1}", controller, action);
        string userName;
        bool isAuthenticated;

        if (User == null)
        {
            userName = "TestUser";
            isAuthenticated = false;
        }
        else
        {
            userName = User.Identity.Name;
            isAuthenticated = User.Identity.IsAuthenticated;
        }

        PageSetting setting = personalizationProcess.GetPageSetting(userName, isAuthenticated, path);
        PageModel model = new PageModel();
        model.Act = action;
        model.Con = controller;
        model.IsEditing = isEditing;
        model.PageSetting = setting;
        return model;
A: 

Without looking at the code, is hard to give you specific advice on how to avoid code duplication. But in general you want to "Extract Method/Extract Class" 'till you can't extract any more :)... Also, you can use some of the MVC infrascructure to help you do some of the repetitive code ie. ModelBinders and ActionFilters.

Maybe you can post some of the view/edit actions' code to point you in a better direction

Jaime
I've added the code for one view of the portal page. The site has to be SEO as well, so I can't just render the widgets and make ajax callbacks either.
Simon Hazelton