如何在嵌套和派生类上设置继承属性?

时间:2014-12-08 03:04:29

标签: c# asp.net-mvc entity-framework inheritance derived-class

我有一个HTML表单:

<form method="post" action="PushToDB">
<input name="ThisFood[0].Name" id="ThisFood[0].Name" value="Apples"/>
<input name="ThisFood[0].Description" id="ThisFood[0].Description" value="Sweet"/>
<input name="ThisFood[0].Color" id="ThisFood[0].Color" value="Red"/> </input>
<input name="ThisFood[1].Name" id="ThisFood[1].Name" value="Broccoli"/>
<input name="ThisFood[1].Description" id="ThisFood[1].Description" value="Disgusting"/>
<input name="ThisFood[1].Color" id="ThisFood[1].Color" value="Green"/>
<input name="NotNested" value="ThisWorks">
<input type="submit"/>

我有模特:

public abstract class BaseModel 
{
    [Key]
    [Required]
    public String Name { get; set; }
    public String Description { get; set; }
}
public class ThisFood : BaseModel
{
    public String Color {get; set; }
}
public class Overall {
    public virtual IEnumberable<ThisFood> ThisFood { get; set; }
    public String NotNested { get; set; } 
}

我的控制器:

public ActionResult PushToDB(Overall overall)
{
    // My "overall" input parameter from the post method is not mapping correctly
    // overall always looks something like
    // overall = { ThisFood = null; NotNested = "This works"; }
}

我想要的是输入参数&#34;整体&#34;已经完全映射出来,所以我真正需要做的就是使用实体框架来实现.saveChanges();但是,我无法做到 a)设置嵌套列表元素的属性值(意思是,只有#NotNested输入正确映射) b)设置继承属性的属性值(名称和描述)

我必须动态创建这些元素,因此不能使用嵌入式C#来创建强类型关系。

0 个答案:

没有答案