2

モデルバインダーに以下のメソッドがあります。

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));
        }
    }

create.cshtml ビューで IsSoftDeleted の chebox をチェックすると、モデル バインダーの値は true のみになるはずなのに、"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>
}  
4

1 に答える 1

8

明らかに失敗するメソッドをtrue,false使用して、の文字列値をブール値に解析しようとしています。Convert.ToBooleanこの状況に対処する正しい方法は、フレームワークに既に組み込まれているものを使用するだけです =>メソッドによって返されるConvertToメソッドを使用します。ValueProviderResultGetValue

そのように:

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
    );
}

それでおしまい:

var isSoftDeletedValue = bindingContext.ValueProvider.GetValue("IsSoftDeleted");
bool isSoftDeleted = (bool)isSoftDeletedValue.ConvertTo(typeof(bool));

ここでは、状況を正しく処理する方法を知っているの基になるConvertToメソッドを呼び出していることに注意してください。ValueProviderResult

于 2013-12-31T13:55:27.767 に答える