Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET MVC中绑定属于自定义模型绑定器中另一个对象的列表_C#_Asp.net Mvc_Model - Fatal编程技术网

C# 在ASP.NET MVC中绑定属于自定义模型绑定器中另一个对象的列表

C# 在ASP.NET MVC中绑定属于自定义模型绑定器中另一个对象的列表,c#,asp.net-mvc,model,C#,Asp.net Mvc,Model,我知道有人问过类似的问题,但这可能有点不同 下面是我的事件对象: Event : IEvent public int Id public string Title public List<EventContact> Contacts 因此,一个事件有一个EventContact对象列表。现在,Event还实现了IEEvent——因此是自定义模型绑定器。我使用IEvent而不是Event,因此当默认模型绑定器尝试执行其操作时,它会让我知道它无法创建“IEvent” 我的视

我知道有人问过类似的问题,但这可能有点不同

下面是我的事件对象:

Event : IEvent
  public int Id
  public string Title
  public List<EventContact> Contacts
因此,一个
事件
有一个
EventContact
对象列表。现在,Event还实现了IEEvent——因此是自定义模型绑定器。我使用
IEvent
而不是Event,因此当默认模型绑定器尝试执行其操作时,它会让我知道它无法创建“IEvent”

我的视图中填充了联系人信息:

<input type="text" name="contact[0].Name" value="DB Value"/>
<input type="text" name="contact[1].Name" value="DB Value"/>
<input type="text" name="contact[2].Name" value="DB Value"/>

<input type="text" name="contact[0].Email" value="DB Value"/>
<input type="text" name="contact[1].Email" value="DB Value"/>
<input type="text" name="contact[2].Email" value="DB Value"/>

<!-- Event fields, etc -->


因此,在我的自定义模型活页夹中,我可以看到所有的值-甜蜜!唯一的问题是,我真的不知道如何获取所有联系人字段并从中创建联系人列表,以及绑定所有事件字段。

要完成上述操作,我只是为所有EventContact字段查询了现有绑定上下文的
ValueProvider
,并将其与新绑定上下文一起发送到默认模型绑定器:

IDictionary<string, ValueProviderResult> contactValueProvider = bindingContext.ValueProvider
            .Select(t => new { t.Key, t.Value })
            .Where(t => t.Key.Contains("EventContact"))
            .ToDictionary(t => t.Key, t => t.Value);

ModelBindingContext contactBindingContext = new ModelBindingContext()
        {
            ModelName = "EventContact",
            ModelState = bindingContext.ModelState,
            ModelType = typeof(List<EventContact>),
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = contactValueProvider
        };

_event.Contacts = ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, contactBindingContext) as IQueryable<EventContact>;
IDictionary contactValueProvider=bindingContext.ValueProvider
.Select(t=>new{t.Key,t.Value})
.Where(t=>t.Key.Contains(“EventContact”))
.ToDictionary(t=>t.Key,t=>t.Value);
ModelBindingContext contactBindingContext=新建ModelBindingContext()
{
ModelName=“EventContact”,
ModelState=bindingContext.ModelState,
ModelType=typeof(列表),
PropertyFilter=bindingContext.PropertyFilter,
ValueProvider=联系人ValueProvider
};
_event.Contacts=ModelBinders.Binders.DefaultBinder.BindModel(controllerContext,contactBindingContext)作为IQueryable;

它可以工作,所以我很高兴:p

另外,我下载了MVC源代码并用它进行了调试-如果您处于死胡同,强烈建议您这样做。有趣的东西:)这在MVC2.0中起作用,因为我无法编译示例源代码。此代码是否在事件模型绑定器的BindModel方法中?
IDictionary<string, ValueProviderResult> contactValueProvider = bindingContext.ValueProvider
            .Select(t => new { t.Key, t.Value })
            .Where(t => t.Key.Contains("EventContact"))
            .ToDictionary(t => t.Key, t => t.Value);

ModelBindingContext contactBindingContext = new ModelBindingContext()
        {
            ModelName = "EventContact",
            ModelState = bindingContext.ModelState,
            ModelType = typeof(List<EventContact>),
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = contactValueProvider
        };

_event.Contacts = ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, contactBindingContext) as IQueryable<EventContact>;