模型绑定ValueProvider追加到现有值+ 4 MVC [英] Model binder ValueProvider appends to the existing value + MVC 4
问题描述
我有下面的方法在我的模型绑定:
保护覆盖对象CreateModel(ControllerContext controllerContext,ModelBindingContext的BindingContext,类型modelType)
{
如果(bindingContext.ValueProvider.GetValue(ID)== NULL)
{
字符串s = bindingContext.ValueProvider.GetValue(IsSoftDeleted)AttemptedValue。 布尔D = Convert.ToBoolean(S);
返回OrgFactory.Create(bindingContext.ValueProvider.GetValue(标题)。AttemptedValue,
bindingContext.ValueProvider.GetValue(NameInUse)。AttemptedValue,
bindingContext.ValueProvider.GetValue(描述)。AttemptedValue,
D,新党());
}
其他
{
返回OrgFactory.Create(bindingContext.ValueProvider.GetValue(标题)。AttemptedValue,
bindingContext.ValueProvider.GetValue(NameInUse)。AttemptedValue,
bindingContext.ValueProvider.GetValue(描述)。AttemptedValue,
Convert.ToBoolean(bindingContext.ValueProvider.GetValue(IsSoftDeleted)AttemptedValue。));
}
}
在create.cshtml看来,如果我检查IsSoftDeleted的chebox,其模型绑定值来为真,假时,它应该只真。
u能指教一下我做错了?
create.cshtml
@using PartyBiz.Models.Objects
@model组织@using(Html.BeginForm(创建,组织,FormMethod.Post))
{@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>创建一个新的组织与LT; /传说> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Caption)
@ Html.EditorFor(型号=> model.Caption,新{@class =TXT})
@ Html.ValidationMessageFor(型号=> model.Caption)
< / DIV> < BR /> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.NameInUse)
@ Html.EditorFor(型号=> model.NameInUse,新{@class =TXT})
@ Html.ValidationMessageFor(型号=> model.NameInUse)
< / DIV> < BR /> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Description)
@ Html.EditorFor(型号=> model.Description,新{@class =TXT})
@ Html.ValidationMessageFor(型号=> model.Description)
< / DIV>
< DIV CLASS =编辑标记>
@ Html.LabelFor(O = GT; O.IsSoftDeleted)
@ Html.EditorFor(O = GT; O.IsSoftDeleted)
@ Html.ValidationMessageFor(O = GT; O.IsSoftDeleted)
< / DIV>
< BR />
<输入类型=提交值=创建/>
< /字段集>
}
您正试图解析的字符串值 true和false
来使用布尔的 Convert.ToBoolean
方法,它将超过明显失败。为应对这种情况的正确方法是简单地使用什么是已经内置到框架=>使用的 ValueProviderResult $ c中的
的ConvertTo
方法$ C>将由的GetValue
方法返回。
就这样:
保护覆盖对象CreateModel(ControllerContext controllerContext,ModelBindingContext的BindingContext,类型modelType)
{
ValueProviderResult isSoftDeletedValue = bindingContext.ValueProvider.GetValue(IsSoftDeleted);
//使用内置的方法到模型绑定正确转换
//值到相应的布尔类型
布尔isSoftDeleted =(布尔)isSoftDeletedValue.ConvertTo(typeof运算(布尔)); 如果(bindingContext.ValueProvider.GetValue(ID)== NULL)
{ 返回OrgFactory.Create(
bindingContext.ValueProvider.GetValue(标题)。AttemptedValue,
bindingContext.ValueProvider.GetValue(NameInUse)。AttemptedValue,
bindingContext.ValueProvider.GetValue(描述)。AttemptedValue,
isSoftDeleted,
新党()
);
} 返回OrgFactory.Create(
bindingContext.ValueProvider.GetValue(标题)。AttemptedValue,
bindingContext.ValueProvider.GetValue(NameInUse)。AttemptedValue,
bindingContext.ValueProvider.GetValue(描述)。AttemptedValue,
isSoftDeleted
);
}
就是这样:
VAR isSoftDeletedValue = bindingContext.ValueProvider.GetValue(IsSoftDeleted);
布尔isSoftDeleted =(布尔)isSoftDeletedValue.ConvertTo(typeof运算(布尔));
请注意,我们在这里呼吁底层的ConvertTo
方法 ValueProviderResult
它知道如何正确处理情况
I have the below method in my model binder:
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));
}
}
in create.cshtml view, if I check the chebox for IsSoftDeleted, its value in model binder is coming as "true,false" when it should come only true.
Can u advise what I am doing wrong?
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>
}
You are attempting to parse the string value of true,false
to a boolean using the Convert.ToBoolean
method which will more than obviously fail. The correct way to deal with this situation is to simply use what's already built into the framework => use the ConvertTo
method on the ValueProviderResult
that will be returned by the GetValue
method.
Just like that:
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
);
}
That's it:
var isSoftDeletedValue = bindingContext.ValueProvider.GetValue("IsSoftDeleted");
bool isSoftDeleted = (bool)isSoftDeletedValue.ConvertTo(typeof(bool));
Notice that here we are calling the underlying ConvertTo
method on the ValueProviderResult
which knows how to correctly handle the situation.
这篇关于模型绑定ValueProvider追加到现有值+ 4 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!