What is a clean / dry way to show available operations with user content?

Work with the ASP.NET MVC 3 (Razor) application, which is mainly associated with UGC (user-created content).

I am working on the Q & A area where users can ask questions, others can answer, vote, etc.

As such, I am trying to find a clean way to handle the available operations that the user can do on any given page, based on their role and other factors.

Take the “Detailed Questions Page” script (for example, this page is on a stack overflow).

Any (authenticated) user can:

  • Question / Answer Vote
  • Answer

The question owner can:

  • Edit
  • Delete
  • Mark answer

Etc.

QuestionViewModel, .

AutoMapper.

"" (, ) ?

:

  • : QuestionOperation (, , , , ..)
  • IEnumerable<QuestionOperation> ViewModel
  • (HTTP GET), , , , .
  • , Html.ActionLink

- - ?

, QuestionViewModel :

  • " "
  • " "

/, AutoMapper.

+5
1

, , . Html.Action , .

- :

public class UserLinksController: Controller
{
    // TODO: ctor DI of a repository, etc...

    public ActionResult Index(string questionId)
    {
        string username = User.Identity.IsAuthenticated 
            ? User.Identity.Name : string.Empty;
        var roles = _repository.GetRolesForQuestion(username, questionId);
        var model = Mapper.Map<UserRoles, RolesViewModel>(roles);
        return PartialView(model);
    }
}

:

@model RolesViewModel
@if(Model.CanEdit)
{
    @Html.ActionLink("Edit", "Edit", "Questions")    
}
@if(Model.CanDelete)
{
    @Html.ActionLink("Delete", "Delete", "Questions")    
}
...

- Html.Action:

@Html.Action("Index", "UserLinks", new { questionId = Model.QuestionId })
+1

All Articles