模型绑定器ValueProvider附加到现有值+ MVC 4

mms*_*ann 2 razor asp.net-mvc-4

我的模型绑定器中有以下方法:

protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        if (bindingContext.ValueProvider.GetValue("Id") == null)
        {
            string s = bindingContext.ValueProvider.GetValue("IsSoftDeleted").AttemptedValue;

            bool d = Convert.ToBoolean(s);
            return OrgFactory.Create(bindingContext.ValueProvider.GetValue("Caption").AttemptedValue,
                            bindingContext.ValueProvider.GetValue("NameInUse").AttemptedValue,
                            bindingContext.ValueProvider.GetValue("Description").AttemptedValue,
                            d, new Party());
        }
        else
        {
            return OrgFactory.Create(bindingContext.ValueProvider.GetValue("Caption").AttemptedValue,
                            bindingContext.ValueProvider.GetValue("NameInUse").AttemptedValue,
                            bindingContext.ValueProvider.GetValue("Description").AttemptedValue, 
                            Convert.ToBoolean(bindingContext.ValueProvider.GetValue("IsSoftDeleted").AttemptedValue));
        }
    }
Run Code Online (Sandbox Code Playgroud)

在create.cshtml视图中,如果我检查chebox的IsSoftDeleted,它在模型绑定器中的值将变为"true,false",它应该只是真的.

你能告诉我做错了什么吗?

create.cshtml

@using PartyBiz.Models.Objects
@model Organization

@using (Html.BeginForm("Create", "Organization", FormMethod.Post))
{

@Html.ValidationSummary(true)
<fieldset>
    <legend>Create a New Organization</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Caption) 
        @Html.EditorFor(model => model.Caption, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.Caption) 
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.NameInUse)
        @Html.EditorFor(model => model.NameInUse, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.NameInUse)
    </div> <br />

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
        @Html.EditorFor(model => model.Description, new { @class = "txt"}) 
        @Html.ValidationMessageFor(model => model.Description)
    </div> 
    <div class="editor-label">
        @Html.LabelFor(O => O.IsSoftDeleted)
        @Html.EditorFor(O => O.IsSoftDeleted)
        @Html.ValidationMessageFor(O => O.IsSoftDeleted)
    </div>
    <br />
        <input type="submit" value="Create" />
</fieldset>
}  
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 8

您正在尝试true,false使用Convert.ToBoolean超出明显失败的方法将字符串值解析为布尔值.处理这种情况的正确方法是简单地使用已经构建到框架中的内容=>使用ConvertTo方法ValueProviderResult返回的GetValue方法.

就像那样:

protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
    ValueProviderResult isSoftDeletedValue = bindingContext.ValueProvider.GetValue("IsSoftDeleted");
    // use the built-in method into the model binder to correctly convert
    // the value to the corresponding boolean type 
    bool isSoftDeleted = (bool)isSoftDeletedValue.ConvertTo(typeof(bool));

    if (bindingContext.ValueProvider.GetValue("Id") == null)
    {

        return OrgFactory.Create(
            bindingContext.ValueProvider.GetValue("Caption").AttemptedValue,
            bindingContext.ValueProvider.GetValue("NameInUse").AttemptedValue,
            bindingContext.ValueProvider.GetValue("Description").AttemptedValue,
            isSoftDeleted, 
            new Party()
        );
    }

    return OrgFactory.Create(
        bindingContext.ValueProvider.GetValue("Caption").AttemptedValue,
        bindingContext.ValueProvider.GetValue("NameInUse").AttemptedValue,
        bindingContext.ValueProvider.GetValue("Description").AttemptedValue, 
        isSoftDeleted
    );
}
Run Code Online (Sandbox Code Playgroud)

而已:

var isSoftDeletedValue = bindingContext.ValueProvider.GetValue("IsSoftDeleted");
bool isSoftDeleted = (bool)isSoftDeletedValue.ConvertTo(typeof(bool));
Run Code Online (Sandbox Code Playgroud)

请注意,我们在这里调用底层ConvertTo方法,该方法ValueProviderResult知道如何正确处理这种情况.