2016-07-28 23 views
1

我有一個問題,我認爲我已經正確地設置了事情,但結果總是零。如何使用同一模型中的兩個下拉菜單實例?

這裏的三種型號:

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。該下拉菜單完美工作,並在兩個相關菜單中顯示家庭成員,並且該關係還顯示在relationType下拉菜單中。但是,當我點擊創建所有值設置爲空。

創建控制器:

// 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屬性相匹配的項目出現。 。或dropdownlistfor

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

使用還需要增加一個下拉列表供familyMemberSecondary

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

這應該讓你非常接近..

@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屬性一個失敗的POST

DotNetFiddle Example

+0

太棒了。非常感謝您爲此提供的幫助。 –

1

這是預期的行爲。請記住Http是無狀態的。所以,你需要返回到視圖

[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); 
} 

編輯之前重新加載您的下拉數據:按評論

的FamilyRelationship所以familyMemberPrimary中的值, familyMemberSecondary和關係類型具有值0,其中我 期待這些每個這樣的id將被傳遞。

因爲您在視圖中使用EditorFor幫助方法familyMemberPrimary屬性。因此,如果您沒有填寫該輸入字段中的值,它將具有默認值(0代表int類型)

如果您希望該屬性填充(家庭成員的)下拉選擇,您應該將下拉列表名稱設置爲familyMemberPrimary,這樣當您發佈表單時,模型綁定會將所選選項值設置爲familyMemberPrimary屬性。

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

這是一個很好的觀點,但是這並不能解決我爲什麼沒有獲得任何價值的問題。無論我從三個下拉菜單中選擇什麼,結果都是0,0,0,我似乎無法從每個選擇中獲取id值。 –

+0

你沒有得到什麼?你的HttpPost操作方法的參數是'FamilyRelationship'類型的屬性值不正確? – Shyju

+0

'FamilyRelationship' so'familyMemberPrimary','familyMemberSecondary'和'relationshipType'中的值的值爲0,我期待這些值中的每一個的id都會被傳遞過來。我不確定代碼中哪裏出錯。我知道我犯了一個錯誤,似乎無法看到它。 –

相關問題