gpt4 book ai didi

asp.net-mvc-3 - 内部模型的自定义模型绑定(bind)器

转载 作者:行者123 更新时间:2023-12-04 22:35:01 25 4
gpt4 key购买 nike

我有一个这样的模型:

public class MainModel
{
public string Id {get;set;}
public string Title {get;set;}
public TimePicker TimePickerField {get;set;}
}
TimePicker是一个看起来像这样的内部模型:
public class TimePicker 
{
public TimeSpan {get;set;}
public AmPmEnum AmPm {get;set;}
}

我正在尝试为内部模型创建自定义模型绑定(bind): TimePicker
问题是:如何获取以表单形式提交到 TimePicker 的自定义模型绑定(bind)器中的值模型字段?

如果我尝试这样得到它:
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

我刚得到 nullvalue .

我不确定如何正确实现模型绑定(bind)器。
public class TimePickerModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
var result = new TimePicker();

var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null)
{
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
try
{
//result = Duration.Parse(value.AttemptedValue);
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
}
}

return result;
}
}

最佳答案

以下对我有用。

模型:

public enum AmPmEnum
{
Am,
Pm
}

public class TimePicker
{
public TimeSpan Time { get; set; }
public AmPmEnum AmPm { get; set; }
}

public class MainModel
{
public TimePicker TimePickerField { get; set; }
}

Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MainModel
{
TimePickerField = new TimePicker
{
Time = TimeSpan.FromHours(1),
AmPm = AmPmEnum.Pm
}
};
return View(model);
}

[HttpPost]
public ActionResult Index(MainModel model)
{
return View(model);
}
}

查看( ~/Views/Home/Index.cshtml):
@model MainModel
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.TimePickerField)
<button type="submit">OK</button>
}

自定义编辑器模板 ( ~/Views/Shared/EditorTemplates/TimePicker.cshtml ) 合并了 TimeAmPm属性到单个输入字段中,稍后将需要自定义模型绑定(bind)器以便在提交表单时拆分它们:
@model TimePicker
@Html.TextBox("_picker_", string.Format("{0} {1}", Model.Time, Model.AmPm))

和模型粘合剂:
public class TimePickerModelBinder : IModelBinder
{
public object BindModel(
ControllerContext controllerContext,
ModelBindingContext bindingContext
)
{
var key = bindingContext.ModelName + "._picker_";
var value = bindingContext.ValueProvider.GetValue(key);
if (value == null)
{
return null;
}

var result = new TimePicker();

try
{
// TODO: instead of hardcoding do your parsing
// from value.AttemptedValue which will contain the string
// that was entered by the user
return new TimePicker
{
Time = TimeSpan.FromHours(2),
AmPm = AmPmEnum.Pm
};
}
catch (Exception ex)
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName,
ex.Message
);
// This is important in order to preserve the original user
// input in case of error when redisplaying the view
bindingContext.ModelState.SetModelValue(key, value);
}
return result;
}
}

最后在 Application_Start 中注册您的模型活页夹:
ModelBinders.Binders.Add(typeof(TimePicker), new TimePickerModelBinder());

关于asp.net-mvc-3 - 内部模型的自定义模型绑定(bind)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993626/

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