ValueProviderResult的AttemptedValue返回错误的值

时间:2014-10-01 23:39:40

标签: c# asp.net-mvc model-binding custom-model-binder

我在我的项目中使用了ASP.NET MVC5,并且我在其中使用了自定义模型绑定。 当我在模型中获得IsActive属性的值时,我遇到了一个问题.IsActive值必须是" true"或"假"但我得到了" true,false" 我怎样才能得到正确的值?

public class BaseModelBinder<TModel> : IModelBinder where TModel : class, new()
{
    public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        TModel model = (TModel)bindingContext.Model ?? new TModel();
        ICollection<string> propertyNames = bindingContext.PropertyMetadata.Keys;
        foreach (var propertyName in propertyNames)
        {
            var value = GetValue(bindingContext, propertyName);
            model.SetPropertyValue(propertyName, value);
        }
        return model;
    }

    private string GetValue(ModelBindingContext context, string name)
    {
        name = (context.ModelName == "" ? "" : context.ModelName + ".") + name;

        ValueProviderResult result = context.ValueProvider.GetValue(name);
        if (result == null || result.AttemptedValue == "")
        {
            return "<Not Specified>";
        }
        else
        {
            return (string)result.AttemptedValue;
        }
    }
}

AttemptedValue of ValueProviderResult returns wrong value

  

我的观点摘要:

@using System.ComponentModel
@using Kendo.Mvc.UI
@model Jahan.Blog.ViewModel.ArticleViewModel
  @{
         ViewBag.Title = "Edit";
    }
<h2>Edit</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Article</h4>
    <hr />
    @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.IsActive)
            @Html.ValidationMessageFor(model => model.IsActive)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.IsActiveNewComment, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(model => model.IsActiveNewComment)
            @Html.ValidationMessageFor(model => model.IsActiveNewComment)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

2 个答案:

答案 0 :(得分:1)

您尚未查看,但我怀疑您使用CheckBoxFor()使用相同名称呈现2个输入,第一个是复选框,第二个是隐藏输入(由于未选中,因此使用复选框不会回发,这可以确保发布默认值)。 DefaultModelBinder如果存在,则忽略第二个值。

答案 1 :(得分:-1)

根据Stephen Muecke's descriptions,我修改了BindModel方法。

public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        TModel model = (TModel)bindingContext.Model ?? new TModel();
        ICollection<string> propertyNames = bindingContext.PropertyMetadata.Keys;
        foreach (var propertyName in propertyNames)
        {
            string value = GetValue(bindingContext, propertyName);
            if (bindingContext.PropertyMetadata[propertyName].ModelType.Name == "Boolean")
            {
                if (value.ToLower() == "true,false" || value.ToLower() == "false,true")
                    value = System.Web.HttpContext.Current.Request.Form.GetValues(propertyName).First();
            }
            model.SetPropertyValue(propertyName, value);
        }
        return model;
    }