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

查看:29
本文介绍了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);

但是对于上面的场景,您的实体包含其他复杂类型呢?是否有用于绑定 IEnumerableT> 的内置机制?到下拉列表,然后在提交表单时自动添加正确的 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.

推荐答案

请查看 this,因为我认为这谈到了您的问题,似乎 S#arp 的人已经解决了它并且很容易撕裂"'如果你不用他们的东西.

Please check this, as I think that talks about your question, it seems that the S#arp guys have solved it and it's easily "rip"'able if you don't use their stuff.

无论如何,我认为从用户发布的任何内容中自动加载实体有点危险......必须考虑一下.

Anyway I think it's somewhat dangerous to automatically load entities from whatever the user posts... will have to think about it.

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

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