Question

Hi I am trying to see if anybody has a briliant idea on how to implement View mode concept in MVC. So if the user opens a page, the page should open in view mode (all controls disabled), if they dont have edit priviledges else should open as normal. Please note that the View page has partial pages as well

Was it helpful?

Solution

I think you have total control on your page under MVC framework. If you are using standard MVC method to generate input controls, you could do following ways.

@Html.TextBox("MyTextBoxID", Model==null?"":Model.MyFieldValue, new {disabled = "disabled})

If you are not using standard MVC method to generate input controls. You can create your own method to generate input controls. For example in MyExt.cs

public static class MyExt
{
    public static MvcHtmlString MyTextBox(this HtmlHelper html, string id, object value)
    {
            // check user privilege
            if (CurrentUser.CanEditThisPage /*Implement your own logic here */)
                 return html.TextBox(id, value);
            else
                 return html.TextBox(id, value, new {disabled = "disabled"});
    }
}

And in your page

@using MyNamespace
...
@Html.MyTextBox("MyTextBoxID", Mode==null?"":Model.MyFieldValue)

Another Way

Pass an indicator from server side to client side and using javascript or JQuery to disable all controls.

@Html.Hidden("CanEdit", CurrentUser.CanEditThisPage)

In javascript

void pageLoad() {
    if ($("#CanEdit").val() == "true"))
        $("input").attr("disabled", "disabled");
}

Something like that (not sure about correctness of syntax :P)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top