如何将DropDownList绑定到ASP.NET MVC中的枚举

时间:2019-05-02 11:29:27

标签: c# asp.net asp.net-mvc enums binding

我想在视图中绑定枚举。我为枚举类创建一个单独的模型。我想绑定“血型”,并且需要一个下拉菜单。

我的捐赠人模型如下。

public class Donor
{
    [Key]
    public int Id { get; set; }
    public string FullName { get; set; }
    public int PhoneNumber { get; set; }
    public DateTime TimeofCreation { get; set; }
    public string Emergency { get; set; }
    public string Address { get; set; }
    public BloodType BloodList    { get; set; }
    public string BagsNumber { get; set; }
    public string LastTimeDonation { get; set; }

}

DonorEnum模型如下

  public class DonorEnum
  {
      public enum BloodType
      {
        OPositive,
        ONegative,
        APositive,
        ANegative,
        BPositive,
        BNegative,
        ABPositive,
        ABNegative

      }
  } 

视图类如下。

  @model BBProjectnew.Models.Donors.DonorCreationModels

 @{
     ViewBag.Title = "CreateR";
  }

  <h2>Create</h2>

 @using (Html.BeginForm()) 
 {
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>DonorCreationModels</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" })
        </div>

   <div class="col-md-10">
            @Html.EditorFor(model => model.BloodType, new { htmlAttributes 
   = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.BloodType, "", new { 
    @class = "text-danger" })
        </div>

我想要一个下拉菜单,用于“血液类型”。创建时如何在视图中实现?

0 个答案:

没有答案