asp.net-mvc - html helper @enumdropdownlistfor 没有将值发送到模型
问题描述
enumdropdownlist
仅用于字符串而不是整数。
我想dropdown
在视图中显示管理员管理。
如果用户选择 admin 的管理后台值为 1,则发送到模型 如果用户选择管理的管理后台值为 2,则发送到模型。
enumdropdownlist
带有字符串而不是整数。
我想要带有整数的模型希望任何人都能理解。
看看这是我的模型
public class tbl_Login
{
public string userid { get; set; }
public string pass { get; set; }
public string Phone { get; set; }
public roles userrole { get; set; }
}
public enum roles
{
Management,
Admin
}
这是我的看法。
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
<div class="form-group has-feedback">
@Html.EnumDropDownListFor(Model => Model.userrole,"Select",new { @class =
"form-control" })
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="col-xs-4">
<input type="submit" value="Sign In" class="btn btn-primary btn-block btn-
flat" />
</div>
}
而不是 asp.net 代码
<select class="form-control">
<option>--Select--</option>
<option>Admin</option>
<option>Management</option>
</select>
最后这是我的控制器。
// I want this controller with log come with value i.e admin=1 or management=2.
public ActionResult Login(tbl_Login log)
{
string uri = Request.Url.AbsoluteUri;
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter adp = new SqlDataAdapter();
if (log.userrole == 'Admin')
{
...
}
return View();
}
我想在控制器中比较这个
if(log.userrole==1)
{
}
解决方案
编辑:
在您的控制器中,您可以这样做:
if ((int)role == 1)
{
// your logic here ...
}
关于枚举以及如何处理它们的一些信息:
像这样将整数值转换为枚举:
roles role = (roles)yourIntValue;
或者像这样
roles role= (roles)Enum.ToObject(typeof(roles) , yourIntValue);
从一个字符串你可以按照以下方式做一些事情:
roles role= (roles) Enum.Parse(typeof(roles), yourStringValue);
推荐阅读
- java - getChildren() add() 和 add()
- node.js - 带有 --target node --bundle-node-modules 的 ParcelJS 无法解析依赖关系
- c# - 如何比较 razor mvc 中的两个日期?
- php - PHP/MySQL - 在哪里可靠地处理插入自动重新请求(锁定表) - 客户端或服务器?
- sql - 如何在postgres中比较两个时间和选择时间小
- python - 尝试在序列化程序“xxx”上获取字段“xxx”的值时出现 AttributeError
- c++ - 如何在for循环C ++的进一步迭代中使用for循环的结果
- pytorch - Windows 不支持 CUDA IPC Ops?有解决方法吗?
- slack - 如何获得与工作区无关的 Slack 机器人令牌?
- r - 无法安装 rlang > 0.2.0