Asp.net mvc 3 在其后面强制转换动态属性';s已过帐(模型绑定)

Asp.net mvc 3 在其后面强制转换动态属性';s已过帐(模型绑定),asp.net-mvc-3,Asp.net Mvc 3,我有一个模型,它有一个动态属性。此动态属性包含大约50个不同对象中的一个。此模型被发送到一个视图,该视图根据使用的对象动态创建页面。这是完美的工作。。。问题是回发。当模型发回时,modelbinder无法绑定动态属性。我期待着这一点,并认为我能够处理它,但我试图从每一个不同的对象采取行动没有任何效果 模型 这很有效 public ActionResult List_Person(VM_List Model, VM_Person_List SearchData) { Model.SearchDat

我有一个模型,它有一个动态属性。此动态属性包含大约50个不同对象中的一个。此模型被发送到一个视图,该视图根据使用的对象动态创建页面。这是完美的工作。。。问题是回发。当模型发回时,modelbinder无法绑定动态属性。我期待着这一点,并认为我能够处理它,但我试图从每一个不同的对象采取行动没有任何效果

模型

这很有效

public ActionResult List_Person(VM_List Model, VM_Person_List SearchData)
{
Model.SearchData = SearchData;
//Stuff
}

public ActionResult List_Car(VM_List Model, VM_Car_List SearchData)
{
Model.SearchData = SearchData;
//Stuff
}
但我想要的是一个单一的行动

public ActionResult List(VM_List Model)
{
    //Stuff
}
我试过类似的方法

public ActionResult List(VM_List Model)
{

        switch (Model.CIType)
        {
            case Config.CIType.Person:
                UpdateModel((VM_Person_List)Model.SearchData);
                break;
            default:
                SearchData = null;
                break;
        }

    //Stuff
}
还有一个定制的活页夹

CIType CIType = (CIType)bindingContext.ValueProvider.GetValue("CIType").ConvertTo(typeof(CIType));

switch (CIType)
{
            case Config.CIType.Person:
                SearchData = (VM_Person_List)bindingContext.ValueProvider.GetValue("SearchData").ConvertTo(typeof(VM_Person_List));
                break;
            default:
                SearchData = null;
                break;
        }

但我两个都不能工作。有什么想法吗?

在尝试了许多不同的方法后,我终于找到了一种有效的方法

行动:

public ActionResult List(VM_List Model)
{

//If the defaultmodelbinder fails SearchData will be an object
if(Model.SearchData.GetType() == typeof(object))
{

//Get SearchData as a Dictionary
Dictionary<string, string> DSearchData = Request.QueryString.AllKeys.Where(k => k.StartsWith("SearchData.")).ToDictionary(k => k.Substring(11), k => Request.QueryString[k]);

switch (Model.CIType)
{
                case Config.CIType.Person:
                    Model.SearchData = new VM_Person_List(DSearchData);
                    break;
                case Config.CIType.Car:
                    Model.SearchData = new VM_Car_List(DSearchData);
                    break;
}

//Rest of action
//..
        }
public ActionResult列表(VM\u列表模型)
{
//如果defaultmodelbinder失败,SearchData将成为一个对象
if(Model.SearchData.GetType()==typeof(对象))
{
//将SearchData作为字典获取
字典DSearchData=Request.QueryString.AllKeys.Where(k=>k.StartsWith(“SearchData”)).ToDictionary(k=>k.Substring(11),k=>Request.QueryString[k]);
交换机(Model.CIType)
{
case Config.CIType.Person:
Model.SearchData=新的虚拟机人员列表(DSearchData);
打破
case Config.CIType.Car:
Model.SearchData=新的虚拟机车辆列表(DSearchData);
打破
}
//行动的其余部分
//..
}
对于每个对象,创建一个接受字典的构造函数

public VM_Car_List(Dictionary<string, string> DSearchData)
{
    this.Make = Convert.ToInt32(DSearchData["Make"]);
    this.Model = Convert.ToInt32(DSearchData["Model"]);
    this.Year = Convert.ToInt32(DSearchData["Year"]);
    // ETC
}
publicvm\u Car\u列表(字典DSearchData)
{
this.Make=Convert.ToInt32(DSearchData[“Make”]);
this.Model=Convert.ToInt32(DSearchData[“Model]”);
this.Year=Convert.ToInt32(DSearchData[“Year”]);
//等
}

当您使用CIType时,您在第三个或第四个示例中是如何定义它的?您不应该像在示例1或2中那样将其定义为模型的一部分吗?我遗漏了“模型”当我简化代码时。添加了它。你能让SearchData字段通用而不是动态吗?或者可能使用接口吗?必须考虑50个具体的类实现是一个严重的代码问题,它们不能通用。通用属性在VM_列表中。“SearchData”是非通用字段。对于汽车,它将是Make、Model、CC、Year和一个人的名字、姓氏、生日、电子邮件等。50个视图模型很好,没有绕过它的余地……使用动态字段,我不需要50个视图(我有一部分工作正常)和50个操作(这是不工作的部分)
public VM_Car_List(Dictionary<string, string> DSearchData)
{
    this.Make = Convert.ToInt32(DSearchData["Make"]);
    this.Model = Convert.ToInt32(DSearchData["Model"]);
    this.Year = Convert.ToInt32(DSearchData["Year"]);
    // ETC
}