2

I am working on ASP.NET (MVC3 HTML5) web site. I need somehow to allow admin to edit content like news, homepage text, promotions etc. Can i implement this using existing API?

Thank you.

1
  • Does an admin need to do this on the frontend of the website? or through an content management system? Commented Apr 16, 2011 at 11:40

1 Answer 1

1

Very simple. Make an interface for editing the content you want the admin to edit and protect it with the [Authorize] attribute

    //for the users    
    [Authorize]
    public ActionResult NormalUsers(int newsItemId)
    {
        //Getting content from DB.
        NewsItem news = new NewsItem(newsItemId);
        return View("ShowNews", news);
    }

    //for editors
    [Authorize(Roles = "Admin, Super User")]
    [HttpGet]
    public ActionResult AdministratorsOnly(int newsItemId)
    {
        //Getting content from DB
        NewsItem news = new NewsItem(newsItemId);
        return View("EditNews", news);
    }

    [Authorize(Roles = "Admin, Super User")]
    [HttpPost]
    public ActionResult AdministratorsOnly(NewsItem newsItem)
    {
        //Putting content in DB
        newsRepository.StoreNewsItemInDB(newsItem);
        NewsItem news = new NewsItem(newsItem.Id);//getting the newsItem from DB, to allow for server side processing. 
        return View("EditNews", news);
    }

Link to MSDN for the language details.

The way it could work is that you have two(actually three) views for news. The first view is for presenting the NewsItem-object for the common user.

The second views are for getting the NewsItem-object for editing. And the third view is for showing the NewsItem-object after editing, to ensure the end result of the editing.

The users will always be presented with the last edited NewsItem(the same as 3).

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, this is right, but how can I specify that admin is editing that page, that will be displayed to user?
It is two different pages, where one of them is read only for users and the other edits the same content( in database, CRM, XML or whatever you fancy for saving content). Your users will never get to the View for admins and thus can not edit. The other advantage is that you can style them differently. Pretty for users and bussiness-like for admin( they love that:-) ).
But what if in the future I will need some other content to be dynamic? Is there a way to directly edit user view from admins?
It's quite simple. you have one View for showing content and another for editing. and do that for every kind of content you need to be edited. So answer to you last question is Yes. :-)
I didn`t get it this time either. Ex. I have News.cshtml(for user) and NewsEditing.cshtml(for admin) and there are two corresponding actions in the controller. What happens when admin edits the content? How this affects News.cshtml? Sorry for bothering you so long :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.