2013-04-30 32 views
0

我有以下類提前綁定在MVC

public class Lookup 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
    ... 
} 

public class AccountTypeLookUp: Lookup 
{  
    ... 
} 

[Serializable] 
public class Account 
{ 
    [HiddenInput] 
    public int AccountId { get; set; } 
    [Required, Display(Name="Account Name", Order=1)] 
    public string AccountName { get; set; } 
    [Display(Name="Account Description", Order=2)] 
    public string AccountDescription { get; set; } 
    [Required, Display(Name="Institution Name")] 
    public string InstitutionName { get; set; } 
    [Required, Display(Name="Account Number"), RegularExpression(@"[0-9]+",ErrorMessage="{0} can only be digits")] 
    public string AccountNumber { get; set; } 
    [Display(Name = "Routing Number"), RegularExpression(@"[0-9]+", ErrorMessage = "{0} can only be digits")] 
    public string RoutingNumber { get; set; } 
    [Required, Display(Name = "Account Type")] 
    public AccountTypeLookup AccountType { get; set; } 
    [ScaffoldColumn(false)] 
    public List<Transaction> TransactionCollection { get; set;} 

    public Account() 
    { 
     AccountId = default(int); 
     TransactionCollection = new List<Transaction>(); 
    } 

現在我認爲我有這顯示帳戶類型的具有等於標識

<select class="input-validation-error" data-val="true" data-val-required="The Account Type field is required." id="AccountType" name="AccountType"> 
<option value="1">Savings</option> 
<option selected="selected" value="2">Checking</option> 
<option value="3">CreditCard</option> 
</select> 

值說明現在如何組合框我是否可以將該組合框綁定到本地變量

account.AccountType.Id =? 

我的方法簽名是

[HttpPost] 
    public ActionResult Create(Account account) 

目前我收到錯誤「The value'2'is invalid。」這是有道理的,因爲它正在尋找一種複雜的類型。

回答

2

您有兩種選擇。第一個選項是做你現在正在做的方式,這是不是使用HTML幫助:

<select class="input-validation-error" data-val="true" data-val-required="The Account Type field is required." id="AccountType" name="AccountType"> 
<option value="1">Savings</option> 
<option selected="selected" value="2">Checking</option> 
<option value="3">CreditCard</option> 
</select> 

但是你必須改變idname以下內容。但請注意,AccountType將不具有它的描述。因爲您只能綁定到comboxbox中的單個屬性,並且該屬性將綁定到Id屬性。

id="AccountType_Id" name="AccountType.Id" 

第二個選項是使用DropDownListFor所以你保證結合效果很好。儘管如此,無論是在控制器還是在視圖中,您都必須構建selectlistitem,但最佳實踐要求您將其構建在控制器中並將其傳遞給您的視圖。所以,你可以有這樣的:

@Html.DropDownListFor(m => m.AccountType.Id, Model.AccountTypes) 

型號,雖然是可以被映射回你Account類視圖模型。如果您不想使用視圖模型並直接在視圖上使用Account,則需要在ViewBag中創建列表(Model.AccountTypes)。然後您需要修改DropDownListFor

@Html.DropDownListFor(m => m.AccountType.Id, 
    (IEnumerable<SelectListItem>)ViewBag.AccountTypes) 

// you might be pulling these values from a database, 
// this is just an example of how you will build the list in the controller, 
// you might build this in a for-loop, foreach or linq 
ViewBag.AccountTypes = new List<SelectListItem> { 
      new SelectListItem { Value = "1", Text="Savings"}, 
      new SelectListItem { Value = "2", Text="Checking"}, 
      new SelectListItem { Value = "3", Text="CreditCard"}, 
     }; 
+0

我正在使用html助手。我只是指向m.AccountType而不是m.AccountType.Id – Mike 2013-04-30 23:14:24

1

我覺得MVC模型綁定知道要查詢字符串Controller/Create?accountType.id=3account.AccountType.Id = 3

綁定因此,嘗試改變

<select class="input-validation-error" data-val="true" data-val-required="The Account Type field is required." id="AccountType" name="AccountType">

<select class="input-validation-error" data-val="true" data-val-required="The Account Type field is required." id="AccountType_Id" name="AccountType.Id">

見遞歸模型綁定部分這裏:http://msdn.microsoft.com/en-us/magazine/hh781022.aspx

+0

感謝您的文章。 – Mike 2013-04-30 23:13:01