gpt4 book ai didi

asp.net-mvc-4 - 模型绑定(bind)器 ValueProvider 附加到现有值 + MVC 4

转载 作者:行者123 更新时间:2023-12-04 06:31:03 25 4
gpt4 key购买 nike

我的模型 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));
}
}

在 create.cshtml View 中,如果我选中 IsSoftDeleted 的复选框,它在模型联编程序中的值将变为“true,false”,而它应该只为真。

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

创建.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>
}

最佳答案

您正在尝试使用 Convert.ToBoolean 方法将 true,false 的字符串值解析为 bool 值,这显然会失败。处理这种情况的正确方法是简单地使用框架中已经内置的内容 => 在将由 返回的 ValueProviderResult 上使用 ConvertTo 方法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
);
}

就是这样:

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

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

关于asp.net-mvc-4 - 模型绑定(bind)器 ValueProvider 附加到现有值 + MVC 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com