2016-03-08 84 views
-1

我無法獲取DropDownListFor以綁定到我的模型。 DisplayOrder返回null,VarietyFamilyID始終爲0.我有一個未知數量的對象進行編輯,所以我試圖使用EditorTemplate。DropDownListFor未綁定在EditorTemplate中

EditorTemplate視圖

@model Wysocki_Variety_Management.Models.Image 

<div> 
    @Html.HiddenFor(i=>i.ImageStreamID) 
    @Model.helperString 
    @Html.DropDownListFor(i=>i.DisplayOrder, new SelectList(ViewBag.DisplayOptions, "Key", "Value")) 
    @Html.DropDownListFor(i => i.VarietyFamilyID, new SelectList(ViewBag.Variety, "VarietyFamilyID", "Name")) 
    @Ajax.ActionLink("Try Me", 
         "UpdateImage", 
         "Images", 
         new { s = Model.ImageStreamID, dislplayOrder = Model.DisplayOrder, familyID = Model.VarietyFamilyID }, 
         new AjaxOptions {}) 
</div> 

查看渲染模板

@model List<Wysocki_Variety_Management.Models.Image> 

@using (Html.BeginForm("UpdateImage", "Images")) 

{  
    <div> 
     @Html.EditorForModel() 
    </div> 
} 

控制器動作簽名

public ActionResult UpdateImage(Guid s, int displayOrder,int familyID) 

編輯

不能確定下來表決Ø r評論。當我使用EditorTemplate和@ Html.EditorForModel時,我爲模型的每個成員獲得多個輸入。

<form action="/Images/UpdateImage" method="post"> <div> 
     <div> 
    <input data-val="true" data-val-required="The Guid field is required." name="[0].ImageStreamID" type="hidden" value="e48aab34-0fe6-e511-8ebf-005056c00008" /> 

    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[0].DisplayOrder"><option value="312">Tuber</option> 
<option value="313">Plant</option> 
<option value="314">Marketing</option> 
</select> 
    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[0].VarietyFamilyID"><option value="0">Fianna</option> 
<option value="0">Linzer Delikatesse</option> 
<option value="3">Markies</option> 
<option value="0">Provento</option> 
<option value="5">Roko</option> 
<option value="6">75-11-15 Sprint</option> 
<option value="7">Alegria</option> 

</select> 
    <a data-ajax="true" href="/Images/UpdateImage?s=e48aab34-0fe6-e511-8ebf-005056c00008&amp;dislplayOrder=0&amp;familyID=0">Try Me</a> 
</div><div> 
    <input data-val="true" data-val-required="The Guid field is required." name="[1].ImageStreamID" type="hidden" value="e58aab34-0fe6-e511-8ebf-005056c00008" /> 

    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[1].DisplayOrder"><option value="312">Tuber</option> 
<option value="313">Plant</option> 
<option value="314">Marketing</option> 
</select> 
    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[1].VarietyFamilyID"><option value="0">Fianna</option> 
<option value="0">Linzer Delikatesse</option> 
<option value="3">Markies</option> 
<option value="0">Provento</option> 
<option value="5">Roko</option> 
<option value="6">75-11-15 Sprint</option> 
<option value="7">Alegria</option> 

</select> 
    <a data-ajax="true" href="/Images/UpdateImage?s=e58aab34-0fe6-e511-8ebf-005056c00008&amp;dislplayOrder=0&amp;familyID=0">Try Me</a> 
</div><div> 
    <input data-val="true" data-val-required="The Guid field is required." name="[2].ImageStreamID" type="hidden" value="e68aab34-0fe6-e511-8ebf-005056c00008" /> 

    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[2].DisplayOrder"><option value="312">Tuber</option> 
<option value="313">Plant</option> 
<option value="314">Marketing</option> 
</select> 
    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[2].VarietyFamilyID"><option value="0">Fianna</option> 
<option value="0">Linzer Delikatesse</option> 
<option value="3">Markies</option> 
<option value="0">Provento</option> 
<option value="5">Roko</option> 
<option value="6">75-11-15 Sprint</option> 
<option value="7">Alegria</option><option value="331">A02507-2LB</option> 

</select> 
    <a data-ajax="true" href="/Images/UpdateImage?s=e68aab34-0fe6-e511-8ebf-005056c00008&amp;dislplayOrder=0&amp;familyID=0">Try Me</a> 
</div><div> 
    <input data-val="true" data-val-required="The Guid field is required." name="[3].ImageStreamID" type="hidden" value="e78aab34-0fe6-e511-8ebf-005056c00008" /> 

    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[3].DisplayOrder"><option value="312">Tuber</option> 
<option value="313">Plant</option> 
<option value="314">Marketing</option> 
</select> 
    <select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[3].VarietyFamilyID"><option value="0">Fianna</option> 
<option value="0">Linzer Delikatesse</option> 
<option value="3">Markies</option> 
<option value="0">Provento</option> 
<option value="5">Roko</option> 
<option value="6">75-11-15 Sprint</option> 
<option value="7">Alegria</option> 
+0

你是什麼意思_I有未知數量的對象edit_?您顯示的視圖只呈現一個對象,除非模型爲「IEnumerable 」,在這種情況下,POST方法中的模型也必須爲'IEnumerable ' –

回答

0

在您的視圖模型是IEnumerable<Image>所以爲了當您提交表單綁定,方法必須

[HttpPost] 
public ActionResult UpdateImage(IEnumerable<Image> model) 

和型號將包含3個正確綁定Image對象。

表單回發基於成功的表單控件的namevalue屬性名稱/值對和匹配DefaultModelBinder那些與你的參數和模型屬性。目前,您方法中的參數爲Guid s, int displayOrder, int familyID,並且值可能綁定到這些參數的唯一方法是如果您的表單包含name="s",name="displayOrder"name="familyID"的控件。

請注意,您在EditorTemplate中使用@Ajax.ActionLink()只會回發模型的初始值。如果你想發佈表單控件的值,那麼你需要提交表單。

+0

請參閱更新的答案。再讀一遍你的問題,我只能假設你的印象是你的'Ajax.ActionLink()'會將表單控件的值發送給你的方法 - 它不會。 –

+0

感謝您的解釋。有一天我會弄清楚這一切。 –