【问题标题】:html helper @enumdropdownlistfor is not sending the value to the modelhtml helper @enumdropdownlistfor 没有将值发送到模型
【发布时间】:2019-03-13 14:36:26
【问题描述】:

enumdropdownlist 仅用于字符串而非整数。

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

如果用户选择 admin 的 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-mvc post ado.net html-helper html.dropdownlistfor


【解决方案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);

【讨论】:

  • @Abdulmubeen,您可以使用我提供的解决方案吗?还有什么不清楚的地方吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多