根据用户角色显示MVCHtmlString

时间:2018-08-07 05:06:52

标签: asp.net-mvc model-view-controller authorization mvchtmlstring

例如,我有一个类似于以下表格的搜索表格,我想检查您是否有权查看输入(用户可能只能看到其中一个,而另一个可能看到其中三个),这是什么?是检查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 个答案:

答案 0 :(得分:0)

控制器和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);
    }

查看

@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>