我想将所有控制器和操作及其相应的角色存储在数据库中。因此,我创建了具有级联DropDown列表的视图,其中在操作的下拉列表中填充所有操作,同时在下拉列表中选择相应的控制器。我有以下代码: 的控制器:
public ActionResult Create()
{
var asm = Assembly.GetExecutingAssembly();
var controllerTypes = from d in asm.GetExportedTypes() where typeof(IController).IsAssignableFrom(d) select d;
List<SelectListItem> controllerdrop = new List<SelectListItem>();
foreach (var item in controllerTypes)
{
string cName = item.Name.ToString();
controllerdrop.Add(new SelectListItem() { Value = cName, Text = cName });
}
ViewBag.controller = controllerdrop;
ViewBag.role = ivr.get_all_role();
return View();
}
public ActionResult get_all_action(string ob)
{
Type t = Type.GetType(ob);
MethodInfo[] mi = t.GetMethods();
List<SelectListItem> action = new List<SelectListItem>();
foreach (MethodInfo m in mi)
{
if (m.IsPublic)
if (typeof(ActionResult).IsAssignableFrom(m.ReturnParameter.ParameterType))
{
action.Add(new SelectListItem() { Value = m.Name, Text = m.Name });
}
}
var List = new SelectList(action, "Value", "Text");
return Json(List, JsonRequestBehavior.AllowGet);
}
查看:
<script src="@Url.Content("~/Scripts/jquery-1.7.2.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#drop1").change(function () {
var id = $(this).val();
$.getJSON("/AssignRole/get_all_action/" + id,
function (data) {
var select = $("#drop2");
select.empty();
select.append($('<option/>', {
value: 0,
text: "Select a action"
}));
$.each(data, function (index, data) {
select.append($('<option/>', {
value: data.Value,
text: data.Text
}));
});
});
});
});
</script>
@using (Html.BeginForm())
{
@Html.DropDownList("Controller", ViewBag.controller as SelectList, "Select a Controller", new { id = "drop1" })
<select id="drop2">
</select>
foreach (var list in ViewBag.role)
{
<input type="checkbox" name="role" value=@list.Name /> @list.Name
}
<input type="submit" value="Save" />
}
问题:
Type t = Type.GetType(ob);
Value cannot be null.
Parameter name: typeName
答案 0 :(得分:0)
在你的控制器动作中将参数名称更改为controllername
。
public ActionResult get_all_action(string controllername)
{
Type t = Type.GetType(controllername);
.
.
.
}
修改强>
controllerdrop.Add(new SelectListItem() { Value = cName, Text = cName });
将此更改为
controllerdrop.Add(new SelectListItem() { Value = item.FullName, Text = cName });