在这里得到错误:
ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage");
Run Code Online (Sandbox Code Playgroud)
如何仅允许选择值?即
[ValidateInput(false)]
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage");
ValueProviderResult value2 = bindingContext.ValueProvider.GetValue("ConfirmationMessage2");
}
Run Code Online (Sandbox Code Playgroud) 我有以下控制器操作:
[HttpPost]
public ViewResult DoSomething(MyModel model)
{
// do something
return View();
}
Run Code Online (Sandbox Code Playgroud)
哪里MyModel是这样的:
public class MyModel
{
public string PropertyA {get; set;}
public IList<int> PropertyB {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
所以DefaultModelBinder应该没有问题地绑定它.唯一的事情是我想使用特殊/自定义绑定器进行绑定PropertyB,我也想重用这个绑定器.所以我认为解决方案是在PropertyB之前放置一个ModelBinder属性,当然这不起作用(属性上不允许使用ModelBinder属性).我看到两个解决方案:
要在每个属性上使用动作参数而不是整个模型(我不喜欢,因为模型具有很多属性),如下所示:
public ViewResult DoSomething(string propertyA, [ModelBinder(typeof(MyModelBinder))] propertyB)
Run Code Online (Sandbox Code Playgroud)要创建一个新类型,可以说MyCustomType: List<int>并注册此类型的模型绑定器(这是一个选项)
也许为MyModel创建一个绑定器,覆盖BindProperty以及该属性是否"PropertyB"使用我的自定义绑定器绑定该属性.这可能吗?
还有其他解决方案吗?
我的印象是,ASP.Net Web API中的模型绑定应该支持具有MVC支持的相同最低功能级别的绑定.
拿以下控制器:
public class WordsController : ApiController
{
private string[] _words = new [] { "apple", "ball", "cat", "dog" };
public IEnumerable<string> Get(SearchModel searchSearchModel)
{
return _words
.Where(w => w.Contains(searchSearchModel.Search))
.Take(searchSearchModel.Max);
}
}
public class SearchModel
{
public string Search { get; set; }
public int Max { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我要求它:
http://localhost:62855/api/words?search=a&max=2
Run Code Online (Sandbox Code Playgroud)
不幸的是,模型没有像在MVC中那样绑定.为什么这不像我期望的那样具有约束力?我的应用程序中会有很多不同的模型类型.如果绑定工作就好了,就像在MVC中一样.
model-binding custom-model-binder asp.net-web-api jquery-datatables-editor
我想为DateTime类型编写自己的模型绑定器.首先,我想写一个我可以附加到我的模型属性的新属性,如:
[DateTimeFormat("d.M.yyyy")]
public DateTime Birth { get; set,}
Run Code Online (Sandbox Code Playgroud)
这是简单的部分.但是粘合剂部分有点困难.我想为类型添加一个新的模型绑定器DateTime.我也可以
IModelBinder接口并编写我自己的BindModel()方法DefaultModelBinder和覆盖BindModel()方法我的模型有如上所示的属性(Birth).因此,当模型尝试将请求数据绑定到此属性时,BindModel(controllerContext, bindingContext)将调用我的模型绑定器.一切都好,但是.如何从controller/bindingContext获取属性属性,以正确解析我的日期?我怎样才能到达PropertyDesciptor房产Birth?
由于关注点的分离,我的模型类是在一个没有(也不应该)引用System.Web.MVC程序集的程序集中定义的.设置自定义绑定(类似于Scott Hanselman的示例)属性是禁止的.
我已经创建了一个自定义的MVC Model Binder,每次HttpPost进入服务器都会调用它.但不会被HttpGet要求提出要求.
GET?如果是这样,我错过了什么?QueryString从GET请求?这是我的实施......
public class CustomModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// This only gets called for POST requests. But I need this code for GET requests.
}
}
Run Code Online (Sandbox Code Playgroud)
Global.asax中
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
//...
}
Run Code Online (Sandbox Code Playgroud)
我已经研究过这些解决方案,但它们并不能满足我的需求:
TempData?Name=John&Surname=Doe)感谢@Felipe的帮助.为了防止有人与之斗争,我学到了:
GET请求DefaultModelBinder类我试图找到一些为我需要处理的独特绑定场景构建自定义模型绑定器的示例,但我发现的所有文章都是针对MVC2中不再相关的旧版MVC.我一直在引用DefaultModelBinder源代码,试图对我需要做的事情有一个普遍的感觉,但它比我的场景更复杂,我无法隔离我需要实现的特定逻辑.
我的目标是获取一组Checkbox/Textbox对,并且对于所有Checked对,我想创建Checkbox值和关联Textbox值的键/值对.在聚合这些数据之后,我需要对集合进行一些字符串序列化,这样我就可以将它存储在所需Model类型的字符串属性中.我已经以可管理的格式从表单发送数据,这将允许我将给定的复选框与特定的文本框相关联,这只是找出如何获取所需的所有部分的问题.
有谁知道一些最新的教程可以让我开始构建自定义模型绑定器?
我想用不同的语言创建一个网站.我已经读过我可以创建一个ActionFilter,但我有一个小问题:
我必须创建一个自定义的ModelBinder才能使用英语和德语数字格式(123,456,789.1vs. 123.456.789,1)
public class DecimalModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string key = bindingContext.ModelName;
var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
float outPut;
if (float.TryParse(v, NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out outPut))
return outPut;
return base.BindModel(controllerContext, bindingContext);
}
}
Run Code Online (Sandbox Code Playgroud)
此ModelBinder使用当前文化来决定使用哪种格式.但不幸的是,在ActionFilter改变文化之前就使用了ModelBinder.
如何在ModelBinder变为活动状态之前更改文化?
我开始玩knockout.js并且这样做我使用了FromJsonAttribute(由Steve Sanderson创建).我遇到了自定义属性未执行模型验证的问题.我把一个简单的例子放在一起 - 我知道它看起来像很多代码 - 但基本问题是如何强制在自定义模型绑定器中验证模型.
using System.ComponentModel.DataAnnotations;
namespace BindingExamples.Models
{
public class Widget
{
[Required]
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
using System;
using System.Web.Mvc;
using BindingExamples.Models;
namespace BindingExamples.Controllers
{
public class WidgetController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Widget w)
{
if(this.ModelState.IsValid)
{
TempData["message"] = String.Format("Thanks for inserting {0}", w.Name);
return RedirectToAction("Confirmation");
}
return View(w);
}
[HttpPost]
public ActionResult PostJson([koListEditor.FromJson] Widget w)
{
//the ModelState.IsValid even though …Run Code Online (Sandbox Code Playgroud) 当我的应用程序在使用不同数字格式的小数(例如1.2 = 1,2)的国家/地区使用时,默认模型绑定器会返回类型为double的属性的错误.网站的文化是在我的BaseController中有条件地设置的.
我已经尝试添加自定义模型绑定器并覆盖bindModel函数但我无法看到如何解决错误,因为Culture已经设置回默认的en-GB.
所以我尝试在我的BaseController中添加一个动作过滤器来设置Culture,但不幸的是bindModel似乎在我的动作过滤器之前被触发了.
我怎么能绕过这个?要么让文化不重置自己,要么在bindModel启动之前将其设置回来?
模型进入无效的控制器:
public ActionResult Save(MyModel myModel)
{
if (ModelState.IsValid)
{
// Save my model
}
else
{
// Raise error
}
}
Run Code Online (Sandbox Code Playgroud)
筛选文化设置:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
CultureInfo culture = createCulture(filterContext);
if (culture != null)
{
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
base.OnActionExecuting(filterContext);
}
Run Code Online (Sandbox Code Playgroud)
自定义模型粘合剂:
public class InternationalDoubleModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueResult != null)
{
if (bindingContext.ModelType == typeof(double) …Run Code Online (Sandbox Code Playgroud) asp.net-mvc localization model number-formatting custom-model-binder
我需要实现一种功能,允许用户以任何形式输入价格,即允许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 ×9
modelbinders ×3
c# ×2
localization ×2
arrays ×1
binding ×1
http-get ×1
imodelbinder ×1
json ×1
model ×1