首页 > 解决方案 > 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)
{
}

标签: asp.net-mvcpostado.nethtml-helperhtml.dropdownlistfor

解决方案


编辑:

在您的控制器中,您可以这样做:

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);

推荐阅读