display MVCHtmlString base on user role

89 views Asked by At

for example, I have a search form like below form, I want check if your has access see inputs ( a user may have access to see just one of them and the other may have access to see 3 of them ), what is the best method or extension to check mvchtmlstring


 @using (Html.BeginForm()){



<div class="form-horizontal">
    <h4>Course</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.CourseID)

    <div class="form-group">
        @Html.LabelFor(model => model.CourseID, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.CourseID)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Credits, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Credits, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Credits, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2" for="DepartmentID">Department</label>
        <div class="col-md-10">
            @Html.DropDownList("DepartmentID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Search" class="btn btn-default" />
        </div>
    </div>
</div>}
1

There are 1 answers

2
kblau On

Controller and ViewModel

//namespace Testy20161006.Controllers
public class MiranViewModel
{
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int Credits { get; set; }
    public int DepartmentID { get; set; }
    public List<SelectListItem> Departments { get; set; }
}

[Flags]
public enum TheSelection
{
    Begin = 0x0,
    SelectA = 0x1,
    SelectB = 0x2,
    SelectC = 0x4,
    SelectD = 0x8
}

public class HomeController : Controller
{
    public ActionResult Tut120()
    {
        List<SelectListItem> list = new List<SelectListItem>();
        SelectListItem item = new SelectListItem { Text = "DeptA", Value = "DeptA" };
        SelectListItem item2 = new SelectListItem { Text = "DeptB", Value = "DeptB" };
        MiranViewModel viewModel = new MiranViewModel { CourseID = 1, Credits = 20, DepartmentID = 1, Title = "Math 101" };
        viewModel.Departments = new List<SelectListItem>();
        viewModel.Departments.Add(item);
        viewModel.Departments.Add(item2);

        //Show section A-C but not section D
        TheSelection theSelection = TheSelection.Begin;
        theSelection |= TheSelection.SelectA;
        theSelection |= TheSelection.SelectB;
        theSelection |= TheSelection.SelectC;

        ViewBag.Selection = theSelection;

        return View(viewModel);
    }

View

@model Testy20161006.Controllers.MiranViewModel
@using Testy20161006.Controllers
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut120</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            <div class="form-horizontal">
                <h4>Course</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.CourseID)

                @if ((ViewBag.Selection & TheSelection.SelectA) == TheSelection.SelectA)
                {
                    <div class="form-group">
                        @Html.LabelFor(model => model.CourseID, new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.DisplayFor(model => model.CourseID)
                        </div>
                    </div>
                }
                @if ((ViewBag.Selection & TheSelection.SelectB) == TheSelection.SelectB)
                {
                    <div class="form-group">
                        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
                        </div>
                    </div>
                }
                @if ((ViewBag.Selection & TheSelection.SelectC) == TheSelection.SelectC)
                {
                    <div class="form-group">
                        @Html.LabelFor(model => model.Credits, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Credits, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Credits, "", new { @class = "text-danger" })
                        </div>
                    </div>
                }
                @if ((ViewBag.Selection & TheSelection.SelectD) == TheSelection.SelectD)
                {
                    <div class="form-group">
                        <label class="control-label col-md-2" for="DepartmentID">Department</label>
                        <div class="col-md-10">
                            @Html.DropDownListFor(m => m.DepartmentID,
                        new SelectList(Model.Departments, "Value", "Text"), new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
                        </div>
                    </div>
                }
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Search" class="btn btn-default" />
                    </div>
                </div>
            </div>}
    </div>
</body>
</html>