我想在数据库中存储所有控制器和动作及其对应的角色。因此,我创建了具有级联下拉列表的视图,其中所有操作都填充在操作的下拉列表中,同时在下拉列表中选择了相应的控制器。我有以下代码: 控制器:
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