我正在寻找一套在实施时使用的最佳实践IModelBinder.
我已经阅读了三本不同的MVC书籍,每一本都在他们的实现中做了一些略有不同的东西而没有任何真正的解释.
IModelBinderDefaultModelBinder而不是直接实施IModelBinder,但我真的没有看到如何利用这些好处ModelState.SetModelValue()遵循惯例.我只是想确保我的模型绑定器遵循约定,并且我正确地理解了整个ModelBindingContext.
任何提示,技巧,GOOD教程推荐?
我需要知道如何IModelBinder在MVC 4中创建自定义并且它已被更改.
必须实施的新方法是:
bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext);
Run Code Online (Sandbox Code Playgroud) 我需要实现一种功能,允许用户以任何形式输入价格,即允许10美元,10美元,10美元......作为输入.
我想通过为Price类实现自定义模型绑定器来解决这个问题.
class Price { decimal Value; int ID; }
Run Code Online (Sandbox Code Playgroud)
表单包含一个数组或价格作为键
keys:
"Prices[0].Value"
"Prices[0].ID"
"Prices[1].Value"
"Prices[1].ID"
...
Run Code Online (Sandbox Code Playgroud)
ViewModel包含一个Price属性:
public List<Price> Prices { get; set; }
Run Code Online (Sandbox Code Playgroud)
只要用户在Value输入中输入十进制可转换字符串,默认模型绑定器就可以正常工作.我想允许输入"100美元".
我的ModelBinder for Price类型到目前为止:
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Price res = new Price();
var form = controllerContext.HttpContext.Request.Form;
string valueInput = ["Prices[0].Value"]; //how to determine which index I am processing?
res.Value = ParseInput(valueInput)
return res;
}
Run Code Online (Sandbox Code Playgroud)
如何实现正确处理数组的自定义模型Binder?
arrays asp.net-mvc imodelbinder modelbinders custom-model-binder
有没有人有关于asp.net mvc beta中新的IModelBinder的教程的链接?
我无法正确理解它,这已经发生了很大变化.
谢谢
我正在尝试迁移到ASP.Net MVC 2并遇到一些问题.这是一个:我需要直接绑定一个字典作为视图的结果.
在ASP.Net MVC 1中,它使用自定义IModelBinder完美地工作:
/// <summary>
/// Bind Dictionary<int, int>
///
/// convention : <elm name="modelName_key" value="value"></elm>
/// </summary>
public class DictionaryModelBinder : IModelBinder
{
#region IModelBinder Members
/// <summary>
/// Mandatory
/// </summary>
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
IDictionary<int, int> retour = new Dictionary<int, int>();
// get the values
var values = bindingContext.ValueProvider;
// get the model name
string modelname = bindingContext.ModelName + '_';
int skip = modelname.Length;
// loop on …Run Code Online (Sandbox Code Playgroud) 我有一个自定义模型绑定器,它从MEF容器中提取接口的实现.它实现如下:
public class PetViewModelBinder : DefaultModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var petId = bindingContext.ValueProvider.GetValue("id");
var container = controllerContext.HttpContext.Application[MvcApplication.PLUGINS] as CompositionContainer;
Lazy<IPet, IPetMetadata> pet = null;
try
{
pet = container.GetExport(typeof(IPet), petId);
var petVM = new Models.PetViewModel(pet);
bindingContext.ModelMetadata.Model = petVM;
return base.BindModel(controllerContext, bindingContext);
}
catch (Exception)
{
throw;
}
finally
{
container.ReleaseExport(pet);
}
}
Run Code Online (Sandbox Code Playgroud)
当MEF具有导出petId时,这非常有效...但是当导出不存在时返回http状态500(服务器错误).应返回错误消息混淆要求,指示http状态403(禁止).
可以采取哪些措施来捕获错误,更改响应状态,以及不返回内容,还是重新路由Action以处理此情况?
http-status-codes imodelbinder defaultmodelbinder asp.net-mvc-3