2016-07-28 11 views
1

私は問題を抱えていますが、正しく設定したとは思っていますが、結果はまったく問題ありません。同じモデルの2つのドロップダウンメニューインスタンスを使用するにはどうすればよいですか?

はここ3つのモデルをです:だから

public class FamilyMember 
{ 
    [Key] 
    public int id { get; set; } 
    [Required] 
    [Display(Name = "First Name")] 
    public string firstName { get; set; } 
    [Required] 
    [Display(Name = "Surname")] 
    public string surname { get; set; } 
    [Required] 
    [Display(Name = "Date of Birth")] 
    public DateTime dob { get; set; } 

    public virtual FamilyRelationship FamilyRelationship { get; set; } 

    [Display(Name = "Full Name")] 
    public string fullName 
    { 
     get 
     { 
      return string.Format("{0} {1}", firstName, surname); 
     } 
    } 
} 

public class RelationshipType 
{ 
    [Key] 
    public int id { get; set; } 
    [Required] 
    [Display(Name="Relationship Type")] 
    public string relationshipType { get; set; } 
    public virtual FamilyRelationship FamilyRelationship { get; set; } 
} 

public class FamilyRelationship 
{ 
    [Key] 
    public int id { get; set; } 
    [ForeignKey("FamilyMembers")] 
    [Display(Name = "First Family Member")] 
    public int familyMemberPrimary { get; set; } 
    [ForeignKey("FamilyMembers")] 
    [Display(Name = "Second Family Member")] 
    public int familyMemberSecondary { get; set; } 
    [Display(Name = "Relationship Type")] 
    public int relationshipType { get; set; } 
    public virtual ICollection<FamilyMember> FamilyMembers { get; set; } 
    public virtual ICollection<RelationshipType> RelationshipTypes { get; set; } 
} 

、私は正常にFamilyMemberRelationshipTypeにデータを追加し、CRUDが完全に働いています。

問題は、コントローラ/ビューの作成FamilyRelationshipにあります。ドロップダウンは完全に機能し、関連する2つのメニューに家族を表示し、関係はrelationTypeドロップダウンにも表示されます。ただし、作成をクリックすると、すべての値がnullに設定されます。

は、コントローラーを作成します。

// GET: FamilyRelationships/Create 
    public ActionResult Create() 
    { 
     ViewBag.familyMember = new SelectList(db.FamilyMembers, "id", "fullName"); 
     ViewBag.relationship = new SelectList(db.RelationshipTypes, "id", "relationshipType"); 
     return View(); 
    } 

    // POST: FamilyRelationships/Create 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "id,familyMemberPrimary,familyMemberSecondary,relationshipType")] FamilyRelationship familyRelationship) 
    { 
     if (ModelState.IsValid) 
     { 
      db.FamilyRelationships.Add(familyRelationship); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(familyRelationship); 
    } 

は、ビューの作成:

@model FamilyTree.Models.FamilyRelationship 

    @{ 
     ViewBag.Title = "Create"; 
    } 

    <h2>Create</h2> 


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

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

        @Html.DropDownList("familyMember", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.familyMemberSecondary, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.DropDownList("familyMember", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.relationshipType, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.DropDownList("relationship", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.relationshipType, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Create" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 

    @section Scripts { 
     @Scripts.Render("~/bundles/jqueryval") 
    } 

私が間違っているつもりですどこ私に知らせて、可能な場合は、この作業をするために例を提供してください。

答えて

2

@Html.DropDownList("familyMember"あなたは、実際のプロパティ名を使用する必要が

最も可能性の高い

@Html.DropDownList("familyMemberPrimary"

あなたはまた、表示する項目のために一致するviewbagプロパティの名前を変更する必要があると思います。 。またはあなたはまた、これはあなたが非常に近い取得する必要があります。..

@model FamilyTree.Models.FamilyRelationship 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

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

    <div class="form-horizontal"> 
     <h4>FamilyRelationship</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.familyMemberPrimary, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(a => a.familyMemberPrimary, (SelectList)ViewBag.familyMember, new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.familyMemberPrimary, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.familyMemberSecondary, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(a => a.familyMemberSecondary, (SelectList)ViewBag.familyMember, new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.familyMemberSecondary, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.relationshipType, new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(a => a.relationshipType, (SelectList)ViewBag.relationship, new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.relationshipType, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

あなたが後にViewBagのプロパティを再設定してくださいfamilyMemberSecondary

@Html.DropDownListFor(a => a.familyMemberSecondary, (SelectList)ViewBag.familyMember , new { @class = "form-control" }) 

のためのドロップダウンリストを追加する必要が

@Html.DropDownListFor(a => a.familyMemberPrimary, (SelectList)ViewBag.familyMember , new { @class = "form-control" }) 

dropdownlistfor使用失敗したPOST

DotNetFiddle Example

+0

それは素晴らしいです。これを手伝うことに本当に感謝します。 –

1

これは予期された動作です。 Httpはステートレスであることを忘れないでください。 familyMemberSecondaryとrelationshipTypeが値を持っている familyMemberPrimaryのでFamilyRelationship内の値

コメントを1として:だから

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "id,familyMemberPrimary,familyMemberSecondary, 
            relationshipType")] FamilyRelationship familyRelationship) 
{ 
    if (ModelState.IsValid) 
    { 
     db.FamilyRelationships.Add(familyRelationship); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    //Let's reload the data for dropdown. 
    ViewBag.familyMember = new SelectList(db.FamilyMembers, "id", "fullName"); 
    ViewBag.relationship = new SelectList(db.RelationshipTypes, "id", "relationshipType"); 

    return View(familyRelationship); 
} 

EDITビューに戻る前に、あなたのドロップダウンデータをリロードする必要があります0、ここでI は、それぞれのIDが渡されることを期待していました。

あなたのビューでfamilyMemberPrimaryプロパティのEditorForヘルパーメソッドを使用しているため。その入力フィールドに値を入力していない場合は、デフォルト値(int型の場合は0)を持ちます。

このプロパティを(家族の)ドロップダウン選択で設定したい場合は、ドロップダウン名の値をfamilyMemberPrimaryと指定して、フォームをポストすると、モデルバインディングは選択したオプション値をfamilyMemberPrimaryプロパティに設定します。

@Html.DropDownList("familyMemberPrimary", 
      ViewBag.familyMember as IEnumerable<SelectListItem>, 
      htmlAttributes: new { @class = "form-control" }) 
+0

それは良い点ですが、私はなぜ値を得ていないのかという問題を解決しません。私が3つのドロップダウンメニューから何を選択しても、結果は0,0,0であり、各選択肢からid値を得ることはできません。 –

+0

何を手に入れていませんか?あなたのHttpPostアクションメソッドのパラメータは、プロパティ値が正しく来ていないタイプの 'FamilyRelationship'タイプですか? – Shyju

+0

'FamilyRelationship'内の値、' familyMemberPrimary'、 'familyMemberSecondary'および' relationshipType'の値は0です。これらの値のそれぞれの 'id'が渡されることを期待していました。コードのどこが間違っているのかわかりません。私は間違いを犯したことを知っている、ただそれを見るように見えない。 –

関連する問題