ASP.NET MVC:绑定复杂类型到选择 [英] ASP.NET MVC: Binding a Complex Type to a Select

查看:237
本文介绍了ASP.NET MVC:绑定复杂类型到选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有内置的支持,结合复杂的类型,形成元素。

I am trying to work out if there is built-in support for binding complex types to form elements.

要使用一个共同的假设情况:
我有属于一类产品的实体 - 模特看起来像这样:

To use a common hypothetical situation: I have a Product entity that belongs to a Category - the models look something like this:

public class Product
{
    public int ID { get; set; }
    public string Description { get; set; }
    public Category Category { get; set; }
}
public class Category
{
    public int ID { get; set; }
    public string Title { get; set; }
}

创建表单来滋润只包含简单的值类型是好的,简单的使用ASP.Net MVC框架,例如一个新的实体:

Creating a form to hydrate a new entity that only contains simple value types is nice and simple using the ASP.Net MVC framework, e.g.:

public ActionResult Create(Product product);

但是,我们上面的场景,你的实体包含其他复杂类型?是否有内置的机制,结合一个IEnumerable< T >一个下拉列表,然后自动补水正确的 T 当表​​单提交?

But what about the above scenario where your entities contain other complex types? Are there built-in mechanisms for binding an IEnumerable<T> to a drop down list and then automatically hydrating the correct T when the form is submitted?

这将是相当琐碎做手工 - 我只是想确定我可以有免费开箱

It would be fairly trivial to do it manually - I'm just trying to ascertain what I can have for free out of the box.

推荐答案

请参阅here这里和<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">here有关模型的细节结合。

See here and here and here for details about model binding.

这篇关于ASP.NET MVC:绑定复杂类型到选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆