I am trying to pass categorized value (string) into model that requires int value.
I have model AccountViewModel
below:
[Required]
[Display(Name = "Fleet Type")]
public int FleetType { get; set; }
When a user registers for the first time, they must choose the fleet type
I have an AccountController
:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var fleetType = 0;
string fleetTypeStr = ViewBag.FleetType;
switch (fleetTypeStr)
{
case "17' Truck":
fleetType = 1;
break;
case "20' Truck":
fleetType = 2;
break;
default:
fleetType = 0;
break;
}
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
LoginId = model.LoginId,
FirstName = model.FirstName,
LastName = model.LastName,
//FleetType = model.FleetType,
FleetType = fleetType,
};
In Account/Register view:
ViewBag.FleetTypeList = new SelectList(new List<string>
{
"Pickup Truck", "Cargo Vans", "10' Truck", "15' Truck", "17' Truck",
"20' Truck", "26' Truck"
}, "fleetTypeList");
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
// ........
@Html.DropDownListFor(m => m.FleetType, ViewBag.FleetTypeList as SelectList, new { @class = "btn btn-primary btn-lg dropdown-toggle" })
But I get a error message because the ViewBag.FleetTypeList
is list of strings but the datatype FleetType
requires int inside the model.
Please help me!