0

私はこのコードを持っています:

UserProfileを(対応するCRUD(Controlers + Views)を使用して)編集すると、SexType(他のCRUDを使用して編集することもできます)のドロップダウンが表示され、SexTypeテーブルに対応します。

public class UserProfile{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int SexTypeId { get; set; }
public virtual SexType SexType { get; set; }

public class SexType
{
    public int SexTypeId { get; set; }
    public string SexTypeDescription { get; set; }
    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}

 public class UserProfileDBContext : DbContext //This creates the database according to the model
{
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<SexType> SexType { get; set; }
}

私が欲しいもの:もっとシンプルなものが欲しいのですが、なんとかできません:

モデル(またはコントローラー)で性別タイプをハードコーディング(男性/女性)したい。データベースにはありません。データベースは必要ありませんが、「UserProfile」でレコードを作成または更新するたびに、「male/female」オプションを使用してドロップダウンを視覚化するだけです。

それを手伝ってくれませんか。

4

2 に答える 2

2

あなたはあなたの見解でこれを行うことができます:

@Html.DropDownList("SexType", 
    new[] { 
        new SelectListItem() { Text="Male", Value = "M" },  
        new SelectListItem() { Text="Female", Value = "F" }
    }
);
于 2012-07-11T17:50:42.787 に答える
0

ViewModelに新しいプロパティを追加して、UserProfileSexTypesのリストを保持します

public class UserProfile{
  public int Id { get; set; }
  public string Name { get; set; }
  public string Surname { get; set; }
  public int SexTypeId { get; set; }
  public IEnumerable<SelectListItem> SexTypes { get; set; }
}

また、GETアクションメソッドでは、それを入力してビューに戻ることができます

public ActionResult Create()
{
  var model=new UserProfile();
  model.SexTypes = new[]
  {
     new SelectListItem { Value = "M", Text = "Male" },
     new SelectListItem { Value = "F", Text = "FeMale" },         
  }; 
  return View(model);
}

UserProfileそして、 ViewModelに強くバインドされているビューでは、

@Html.DropDownListFor(x => x.SexTypeId, new SelectList(Model.SexTypes,
                                                  "Value","Text"), "Select..")
于 2012-07-11T17:57:08.927 に答える