MVC模型绑定到一个集合,其中集合不以0指数开始 [英] MVC Model Binding to a collection where collection does not begin with a 0 index
问题描述
我想在一个集合内的一个项目的产权执行远程验证。验证工作在集合的第一项确定。 HTTP请求验证方法如下:
/确认/ IsImeiAvailable?ImeiGadgets [0] = .ImeiNumber 123456789012345
然而在其中的URL看起来像下面的第二个项目,验证不工作
/确认/ IsImeiAvailable?ImeiGadgets [1] .ImeiNumber = 123456789012345
现在我pretty确保这样做的原因,是有约束力的,不以零开始的索引集合无法工作。
我的验证方法有一个签名如下:
公共JsonResult IsImeiAvailable([装订(preFIX =ImeiGadgets)] Models.ViewModels.ImeiGadget [] imeiGadget)
由于我传递一个集合中的一个项目我有绑定这样还我真正通过只是一个单一的值。
反正是有,我可以处理这个其他不仅仅是绑定它作为一个普通的旧的查询字符串。
感谢
编辑:这是速战速决拿到IMEI号码可变的,但我宁愿使用模型绑定:
字符串imeiNumber = Request.Url.AbsoluteUri.Substring(Request.Url.AbsoluteUri.IndexOf(=)+ 1);
编辑:这是我的ImeiGadget类:
公共类ImeiGadget
{
公众诠释标识{搞定;组; } [远程(IsImeiAvailable,验证)]
[必需(的ErrorMessage =请为您的手机提供IMEI号码)]
[RegularEx pression(@(\\ D * \\ D){} 15,17的ErrorMessage =一个IMEI号码必须15安培之间包含17位)]
公共字符串ImeiNumber {搞定;组; }
公共字符串制作{搞定;组; }
公共字符串模式{搞定;组; }
}
您可以编写一个自定义的模型绑定:
公共类ImeiNumberModelBinder:IModelBinder
{
公共对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext)
{
VAR MODELNAME = bindingContext.ModelName;
VAR请求= controllerContext.HttpContext.Request;
VAR paramName配置=请求
.Params
。键
.Cast<串GT;()
.FirstOrDefault(
X => x.EndsWith(MODELNAME,StringComparison.OrdinalIgnoreCase)
); 如果(!string.IsNullOrEmpty(paramName配置))
{
返回的BindingContext
.ValueProvider
.GetValue(要求[paramName配置])
.AttemptedValue;
} 返回null;
}
}
,然后将其应用到控制器动作:
公众的ActionResult IsImeiAvailable(
[ModelBinder的(typeof运算(ImeiNumberModelBinder))]字符串imeiNumber
)
{
返回JSON(string.IsNullOrEmpty(imeiNumber),JsonRequestBehavior.AllowGet!);
}
现在的 ImeiGadgets [XXX]
部分将从查询字符串被忽略。
I'm trying to perform remote validation on a property of an item within a collection. The validation works OK on the first item of the collection. The http request to the validation method looks like:
/Validation/IsImeiAvailable?ImeiGadgets[0].ImeiNumber=123456789012345
However on the 2nd item where the url looks like below, the validation doesn't work
/Validation/IsImeiAvailable?ImeiGadgets[1].ImeiNumber=123456789012345
Now I'm pretty sure the reason for this, is that binding wont work on a collection that doesn't begin with a zero index.
My validation method has a signature as below:
public JsonResult IsImeiAvailable([Bind(Prefix = "ImeiGadgets")] Models.ViewModels.ImeiGadget[] imeiGadget)
Because I'm passing an item within a collection I have to bind like this yet what I'm really passing is just a single value.
Is there anyway I can deal with this other than just binding it as a plain old query string.
Thanks
Edit: This is the quick fix to get the Imei variable but I'd rather use the model binding:
string imeiNumber = Request.Url.AbsoluteUri.Substring(Request.Url.AbsoluteUri.IndexOf("=")+1);
Edit: Here is my ImeiGadget class:
public class ImeiGadget
{
public int Id { get; set; }
[Remote("IsImeiAvailable", "Validation")]
[Required(ErrorMessage = "Please provide the IMEI Number for your Phone")]
[RegularExpression(@"(\D*\d){15,17}", ErrorMessage = "An IMEI number must contain between 15 & 17 digits")]
public string ImeiNumber { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
You could write a custom model binder:
public class ImeiNumberModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelName = bindingContext.ModelName;
var request = controllerContext.HttpContext.Request;
var paramName = request
.Params
.Keys
.Cast<string>()
.FirstOrDefault(
x => x.EndsWith(modelName, StringComparison.OrdinalIgnoreCase)
);
if (!string.IsNullOrEmpty(paramName))
{
return bindingContext
.ValueProvider
.GetValue(request[paramName])
.AttemptedValue;
}
return null;
}
}
and then apply it to the controller action:
public ActionResult IsImeiAvailable(
[ModelBinder(typeof(ImeiNumberModelBinder))] string imeiNumber
)
{
return Json(!string.IsNullOrEmpty(imeiNumber), JsonRequestBehavior.AllowGet);
}
Now the ImeiGadgets[xxx]
part will be ignored from the query string.
这篇关于MVC模型绑定到一个集合,其中集合不以0指数开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!