我有一个ViewModel,其中包含多态对象的列表。

public class InputParameters
{
    public InputParameters()
    {
        InputPrompts = new List<IInputPrompt>();
    }

    public string Name { get; set; }
    public string Path { get; set; }

    public IList<IInputPrompt> InputPrompts { get; set; }
}


依次查看以下内容:

public interface IInputPrompt
{
    string Name { get; set; }
    bool IsHidden { get; set; }
    bool IsRequired { get; set; }

    dynamic Value { get; set; }
}
public class TextPrompt : IInputPrompt
{
    public string Name { get; set; }
    public bool IsHidden { get; set; }
    public bool IsRequired { get; set; }
    public dynamic Value { get; set; }
}
public class MultiSelectPrompt : IInputPrompt
{
    public string Name { get; set; }
    public bool IsHidden { get; set; }
    public bool IsRequired { get; set; }
    public dynamic Value { get; set; }

    public MultiSelectList Values
    {
        get
        {
            return new MultiSelectList(((IDictionary<int, string>)Value).ToList(), "Key", "Value");
        }
    }
}


每个派生类型都有一个“编辑器视图模板”,这些视图如下所示:

@model OptionListModelBinding.Models.InputParameters
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
    <fieldset>
        <legend><strong>@Model.Name</strong></legend>
        <div><em>@Model.Path</em></div>
        <div>@Html.EditorFor(p => p.InputPrompts)</div>
        <div><input type="submit" /></div>
    </fieldset>

}
// editor template
@model OptionListModelBinding.Models.InputParameters
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
    <fieldset>
        <legend><strong>@Model.Name</strong></legend>
        <div><em>@Model.Path</em></div>
        <div>@Html.EditorFor(p => p.InputPrompts)</div>
        <div><input type="submit" /></div>
    </fieldset>

}
// editor template
@model OptionListModelBinding.Models.MultiSelectPrompt
@{
    ViewBag.Title = "MultiSelectPrompt";
}

<h2>MultiSelectPrompt</h2>
<div><strong>@Model.Name</strong></div>
<div><em>@Html.ListBox(Model.Name, Model.Values)</em></div>


这一切都很好地呈现:

问题是这样的:

如何将其绑定回模型?我有一个自定义模型活页夹:(请对此代码进行修改)。

    public class InputParameterModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }
        // iterate over the form fields
        foreach (string item in controllerContext.HttpContext.Request.Form.AllKeys)
        {
            // this is where the problem is ... I cannot use the generic here.
            var dog = FromPostedData<string>(bindingContext, item);

        }

        InputParameters userInput = new InputParameters();

        return userInput;
    }

    // Dino Esposito code
    private T FromPostedData<T>(ModelBindingContext context, string id)
    {
        if (String.IsNullOrEmpty(id)) return default(T);
        var key = String.Format("{0}.{1}", context.ModelName, id);
        var result = context.ValueProvider.GetValue(key);
        if (result == null && context.FallbackToEmptyPrefix)
        {
            result = context.ValueProvider.GetValue(id);
            if (result == null) return default(T);
        }

        context.ModelState.SetModelValue(id, result);

        T valueToReturn = default(T);
        try
        {
            valueToReturn = (T)result.ConvertTo(typeof(T));
        }
        catch { }
        return valueToReturn;
    }
}


编辑
我是否提到列表中的项目是在运行时确定的?

编辑
这是报告生成工具的前端。后端服务提供了可用报告的列表以及运行每个报告所需的参数。在编译时这些都不是已知的,并且甚至可以通过重新编译Web门户来更改报告定义。

我可以有可变数量和类型的输入参数。

最佳答案

我认为您不需要自定义活页夹。您只需将视图模型传递回post方法即可:

[HttpPost]
public ActionResult ProcessForm(InputParameters viewModel)
{
    ...
}


默认的MVC活页夹将表单值与InputParameters参数的属性匹配。如果您的视图使用任何特殊形式的变量名,则可能需要在该参数上使用[Bind]属性来指示绑定器必须查找的变量名前缀。

关于c# - 在MVC3中绑定(bind)嵌套集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13486763/

10-17 00:59