Asp.net mvc 基于用户角色显示MVCHtmlString
例如,我有一个搜索表单,如下面的表单,我想检查您是否有权查看输入(用户可能有权查看其中一个,而另一个可能有权查看其中3个),检查mvchtmlstring的最佳方法或扩展是什么Asp.net mvc 基于用户角色显示MVCHtmlString,asp.net-mvc,model-view-controller,authorization,mvchtmlstring,Asp.net Mvc,Model View Controller,Authorization,Mvchtmlstring,例如,我有一个搜索表单,如下面的表单,我想检查您是否有权查看输入(用户可能有权查看其中一个,而另一个可能有权查看其中3个),检查mvchtmlstring的最佳方法或扩展是什么 @使用(Html.BeginForm()){ 课程 @Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.CourseID) @LabelFor(model=>model.CourseID,新的{@
@使用(Html.BeginForm()){
课程
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.CourseID)
@LabelFor(model=>model.CourseID,新的{@class=“controllabel col-md-2”})
@DisplayFor(model=>model.CourseID)
@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
@LabelFor(model=>model.Credits,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Credits,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Credits,“,new{@class=“text danger”})
部门
@DropDownList(“DepartmentID”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.DepartmentID,“,new{@class=“text danger”})
}
控制器和视图模型
//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);
}
//名称空间testy2016 1006.控制器
公共类MiranViewModel
{
public int CourseID{get;set;}
公共字符串标题{get;set;}
公共整数积分{get;set;}
public int DepartmentID{get;set;}
公共列表部门{get;set;}
}
[旗帜]
公共枚举选择
{
Begin=0x0,
选择A=0x1,
选择B=0x2,
选择C=0x4,
选择D=0x8
}
公共类HomeController:控制器
{
公共行动结果(120)
{
列表=新列表();
SelectListItem=new SelectListItem{Text=“DeptA”,Value=“DeptA”};
SelectListItem2=新建SelectListItem{Text=“DeptB”,Value=“DeptB”};
MiranViewModel viewModel=new MiranViewModel{CourseID=1,Credits=20,DepartmentID=1,Title=“Math 101”};
viewModel.Departments=新列表();
viewModel.Departments.Add(项);
viewModel.Departments.Add(第2项);
//显示截面A-C,但不显示截面D
TheSelection TheSelection=选择开始;
theSelection |=theSelection.SelectA;
theSelection |=theSelection.SelectB;
theSelection |=theSelection.SelectC;
ViewBag.Selection=所选内容;
返回视图(viewModel);
}
看法
@model testy2016 1006.Controllers.MiranViewModel
@使用Testy2016 1006.控制器
@{
布局=空;
}
图120
@使用(Html.BeginForm())
{
课程
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.CourseID)
@if((ViewBag.Selection和TheSelection.SelectA)=TheSelection.SelectA)
{
@LabelFor(model=>model.CourseID,新的{@class=“controllabel col-md-2”})
@DisplayFor(model=>model.CourseID)
}
@if((ViewBag.Selection和TheSelection.SelectB)=TheSelection.SelectB)
{
@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
}
@if((ViewBag.Selection和TheSelection.SelectC)=TheSelection.SelectC)
{
@LabelFor(model=>model.Credits,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Credits,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Credits,“,new{@class=“text danger”})
}
@if((ViewBag.Selection和TheSelection.SelectD)=TheSelection.SelectD)
{
部门
@Html.DropDownListFor(m=>m.DepartmentID,
新的选择列表(Model.Departments,“Value”,“Text”),新的{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.DepartmentID,“,new{@class=“text danger”})
}
}
感谢您的回复,但这对我来说不是一个好的解决方案,我有一个大项目,它有很多形式,有很多用户,例如,100个用户,bas(关于用户角色部分搜索字段是否可见,我想要一个扩展来检查所有htmlinstring,不想在我的视图中使用if-else配置目前我唯一能想到的是自动生成您的视图。我会研究这个。自动生成的
//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>