0

enumdropdownlist is used only with strings not integers.

i want to show admin management in dropdown in view.

if user select admin background value of admin which is 1 send to the model if user select management background value of manage which is 2 send to the model.

enumdropdownlist is coming with string not integers.

I want model which is come with integers hope anyone understand.

see this is my model

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
}

this is is my view.

@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>
}

instead of asp.net code

<select class="form-control">
<option>--Select--</option>
<option>Admin</option>
<option>Management</option>
</select>

finally this is my controller.

// 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();
} 

I want to compare this in controller

if(log.userrole==1)
{
}
Aria
  • 3,724
  • 1
  • 20
  • 51

1 Answers1

0

EDIT:

In your controller you can do this:

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

Some information about Enums and how you can handle them:

Cast an integer value to a Enum like this:

roles role = (roles)yourIntValue;

Or like this

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

From a string you could do something along these lines:

roles role= (roles) Enum.Parse(typeof(roles), yourStringValue);
Dimitri
  • 1,185
  • 2
  • 15
  • 37