0

I have the following classes

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

Now in my view I have a combobox which displays the description of Account Type with the values equal to the Id

<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>

Now how can I bind that comboBox to my local variable

account.AccountType.Id =?

My method signature is

[HttpPost]
    public ActionResult Create(Account account)

Currently I get the error "The value '2' is invalid." which makes sense since it is looking for a complex type.

4

2 回答 2

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>

但是您必须将 theid和 the更改name为以下内容。但请注意,AccountType 不会有描述。因为您只能从组合框绑定到单个属性,而这将绑定到该Id属性。

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

您的第二个选项是使用DropDownListFor,这样您就可以保证绑定工作良好。您必须在控制器或视图中构建 selectlistitem,但最佳实践要求您在控制器中构建它并将其传递给您的视图。所以你可以有类似的东西:

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

模型虽然是一个可以映射回你的Account类的视图模型。如果您不想使用视图模型并Account直接在视图上使用,则需要Model.AccountTypes在 ViewBag 中创建列表 ( )。然后你需要修改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"},
        };
于 2013-04-30T01:51:50.093 回答
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

于 2013-04-30T01:44:29.010 回答