3
我試圖在ASPNET 4.5 Webforms中使用新的ModelBinding功能,但沒有成功。ASPNET 4.5 WebForms ModelBinding不與DropDownList
由於某種原因,在提交表單後,Contact.ContactType保持爲空。
型號:
public class Contact
{
public int ContactId { set; get; }
public string Name { set; get; }
public string Phone { set; get; }
public ContactType ContactType { set; get; }
}
public class ContactType
{
public int ContactTypeId { set; get; }
public string Description { set; get; }
public virtual ICollection<Contact> Contacts { set; get; }
}
ASPX:
<asp:FormView ID="FormView1" runat="server"
ItemType="Models.Contact" DataKeyNames="ContactId"
DefaultMode="Insert" InsertMethod="InsertContact" >
<InsertItemTemplate>
<ul>
<li>
<label>Name</label>
<asp:DynamicControl runat="server" id="Name" DataField="Name" Mode="Insert" />
</li>
<li>
<label>Phone</label>
<asp:DynamicControl runat="server" id="Phone" DataField="Phone" Mode="Insert" />
</li>
<li>
<label>Contact Type</label>
<asp:DropDownList ID="ContactType" runat="server" AppendDataBoundItems="true"
ItemType="Models.ContactType" SelectMethod="GetContactTypes"
DataTextField="Description" DataValueField="ContactTypeId">
<asp:ListItem Value="0" Text="Select"></asp:ListItem>
</asp:DropDownList>
</li>
<li>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Insert" Text="Insert" >
</asp:LinkButton>
</li>
</ul>
</InsertItemTemplate>
</asp:FormView>
ASPX.CS:
public void InsertContact(Contact contact)
{
if (ModelState.IsValid)
{
// Save changes here
}
}
我要如何使用ModelBinding上下拉/列表框成功?
你有沒有解決這個問題?如果答案對您有幫助,您應該將其作爲正確的方式進行銷售或選擇發佈最佳方法。 –