html helper @enumdropdownlistfor没有将值发送给模型

问题描述 投票:0回答:1

enumdropdownlist仅用于不是整数的字符串。

我想在dropdown中显示管理员管理。

如果用户选择admin的管理后台值为2发送给模型,用户选择admin的admin后台值为1发送给模型。

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-mvc post ado.net html-helper html.dropdownlistfor
1个回答
0
投票

编辑:

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

if ((int)role == 1)
{
   // your logic here ...
}

有关枚举的一些信息以及如何处理它们:

将整数值转换为Enum,如下所示:

roles role = (roles)yourIntValue;

或者像这样

roles role= (roles)Enum.ToObject(typeof(roles) , yourIntValue);

从字符串中你可以沿着这些方向做一些事情:

roles role= (roles) Enum.Parse(typeof(roles), yourStringValue);
© www.soinside.com 2019 - 2024. All rights reserved.