我想将Enum强制转换为int值,有一个扩展将Enum强制转换为我的服务中的seletListItem,如下所示
public Dictionary<int, string> GetWorkingDays()
{
var days = typeof(WorkDays).ToSelectList();
return days;
}
然后我使用像这样的视图包将其返回到我的视图
ViewBag.WorkingDays = _vendorService.GetWorkingDays().Select(x => new SelectListItem { Value = x.Key.ToString(), Text = x.Value });
现在当其中一个Enum从视图中选择它将int值返回给我的控制器时,我如何将该int转换为我的服务初始化...
谢谢你的时间
这是我的服务
public int AddVendor(string companyname, string addressOne, string addressTwo, string city, string state, string country, string phone, string email, int workingDays)
{
var vendor = new Vendor()
{
CompanyName = companyname,
AddressLineOne = addressOne,
AddressLineTwo = addressTwo,
City = city,
State = state,
Country = country,
Phone = phone,
Email = email,
IsActive = true,
RegistrationDate = DateTime.Now
WorkingDays = //casting here to collect the parameter (int workingDays)
};
_unitOfWork.VendorRepository.Insert(vendor);
_unitOfWork.VendorRepository.Save();
return vendor.Id;
}
这是enum
public enum WorkDays
{
EveryDay, MondayToFriday, MondayToSaturday, FridayToSunday, SaturdayAndSunday
}
这是控制器
[HttpPost]
public ActionResult Create(VendorViewModel model)
{
//
if (!ModelState.IsValid)
{
ViewBag.WorkingDays = _vendorService.GetWorkingDays().Select(x => new SelectListItem { Value = x.Key.ToString(), Text = x.Value });
ViewBag.Error = false;
return View(model);
}
var vendor = _vendorService.AddVendor(model.CompanyName, model.AddressLineOne, model.AddressLineTwo, model.City, model.State, model.Country,model.OpeningTime,model.ClosingTime, model.Phone, model.Email, model.WorkingDays);
if (vendor != 0)
{
_vendorService.AddVendorContact(model.FirstName, model.LastName, model.MobileNumber1, model.MobileNumber2, model.EmailAddress, vendor);
}
return RedirectToAction("Index");
}
我的模特
public class Vendor : BaseDataObject
{
public string CompanyName { get; set; }
public string AddressLineOne { get; set; }
public string AddressLineTwo { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public DateTime RegistrationDate { get; set; }
public bool IsActive { get; set; }
public string OpeningTime { get; set; }
public string ClosingTime { get; set; }
public WorkDays WorkingDays { get; set; }
}
public enum WorkDays
{
EveryDay, MondayToFriday, MondayToSaturday, FridayToSunday, SaturdayAndSunday
}
然后这是视图模型
public class VendorViewModel
{
[Required]
[DisplayName("Company Name")]
public string CompanyName { get; set; }
[Required]
[DisplayName("Address Line One")]
public string AddressLineOne { get; set; }
[DisplayName("Address Line Two")]
public string AddressLineTwo { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
[Phone]
public string Phone { get; set; }
[EmailAddress(ErrorMessage = "Invalid E-Mail Address")]
[Required]
public string Email { get; set; }
[DisplayName("Opening Time")]
public string OpeningTime { get; set; }
[DisplayName("Closing Time")]
public string ClosingTime { get; set; }
[DisplayName("Working Days")]
public int WorkingDays { get; set; }
}
答案 0 :(得分:3)
你可以按如下方式投射!!
int x = (int)WorkDays.MondayToFriday;
如果你想要扩展方法,那么你可以这样写: -
public static class EnumExtensions
{
public static int ToInt(this Enum enum)
{
return (int)((object)enum);
}
}
使用它如下所示: -
int x = WorkDays.MondayToFriday.ToInt();
答案 1 :(得分:0)
我相信这就是你想要的。我认为你实际上想要将int转换为枚举而不是相反。将int转换为枚举与转换任何数据类型相同。的(YourEnum)intVar 强>
public int AddVendor(string companyname, string addressOne, string addressTwo, string city, string state, string country, string phone, string email, int workingDays)
{
var vendor = new Vendor()
{
// skipped for brevity
WorkingDays = (WorkDays)workingDays
};
_unitOfWork.VendorRepository.Insert(vendor);
_unitOfWork.VendorRepository.Save();
return vendor.Id;
}
[HttpPost]
public ActionResult Create(VendorViewModel model)
{
//... skipped for brevity
var vendor = _vendorService.AddVendor(model.CompanyName, model.AddressLineOne, model.AddressLineTwo, model.City, model.State, model.Country,model.OpeningTime,model.ClosingTime, model.Phone, model.Email, (int)model.WorkingDays);
//... skipped for brevity
}
如果你将workingDays参数从int workingDays参数更改为枚举参数,那么你就不需要进行强制转换。 E.g。
public int AddVendor(string companyname, string addressOne, string addressTwo, string city, string state, string country, string phone, string email, int workingDays)
更改为
public int AddVendor(string companyname, string addressOne, string addressTwo, string city, string state, string country, string phone, string email, WorkDays workingDays)
{
var vendor = new Vendor()
{
// skipped for brevity
WorkingDays = workingDays
};
_unitOfWork.VendorRepository.Insert(vendor);
_unitOfWork.VendorRepository.Save();
return vendor.Id;
}