ASP.NET MVC中的模型绑定很棒,但它遵循区域设置.在我的语言环境中,小数点分隔符是逗号(','),但用户也使用点('.'),因为它们懒得切换布局.我想在一个地方为decimal我的模型中的所有字段实现这个.
我应该为decimal类型实现自己的Value Provider(或事件Model Binder),还是我错过了一些简单的方法来做到这一点?
我希望能够从cookie中获取键/值并使用它来绑定模型.
我认为DefaultModelBinder不是构建自定义的ModelBinder,而是开箱即用,选择值来自哪里的最佳方法是设置它使用的IValueProvider.
为此,我不想创建自定义ValueProviderFactory并将其全局绑定,因为我只希望在特定的操作方法中使用此ValueProvider.
我已经构建了一个执行此操作的属性:
/// <summary>
/// Replaces the current value provider with the specified value provider
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class SetValueProviderAttribute : ActionFilterAttribute
{
public SetValueProviderAttribute(Type valueProviderType)
{
if (valueProviderType.GetInterface(typeof(IValueProvider).Name) == null)
throw new ArgumentException("Type " + valueProviderType + " must implement interface IValueProvider.", "valueProviderType");
_ValueProviderType = valueProviderType;
}
private Type _ValueProviderType;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
IValueProvider valueProviderToAdd = GetValueProviderToAdd();
filterContext.Controller.ValueProvider = valueProviderToAdd;
}
private IValueProvider GetValueProviderToAdd()
{ …Run Code Online (Sandbox Code Playgroud) 我想发送一个HTTP POST请求,其中包含组成简单博客帖子的信息,没什么特别的.
我读过这里,当你想绑定复杂类型(即一个类型,是不是string,int在网络API等),一个好方法是创建一个自定义模型粘合剂.
我有一个自定义模型binder(BlogPostModelBinder),后者又使用自定义Value Provider(BlogPostValueProvider).我不明白的是,我将如何以及在何处从请求正文中检索数据BlogPostValueProvider?
在模型绑定器内部,我认为这是检索标题的正确方法.
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
...
var title= bindingContext.ValueProvider.GetValue("Title");
...
}
Run Code Online (Sandbox Code Playgroud)
而BlogPostValueProvider看起来像这样:
public class BlogPostValueProvider : IValueProvider
{
public BlogPostValueProvider(HttpActionContext actionContext)
{
// I can find request header information in the actionContext, but not the body.
}
public ValueProviderResult GetValue(string key)
{
// In some way return the value from the body with the given key.
}
}
Run Code Online (Sandbox Code Playgroud)
这可能是一种更容易解决的方法,但是因为我正在探索Web API,所以最好让它工作. …
我想知道ValueProvidersASP.NET MVC中不同执行的顺序.
ValueProviders:
我没有找到相关信息.
在我的项目中,我希望允许用户以2种格式输入双值:使用','或'.' 作为分隔符(我对指数形式不感兴趣).默认值为分隔符'.' 不工作.我希望这种行为适用于复杂模型对象中的所有双属性(目前我使用的是包含标识符和值的对象集合).
我应该使用什么:价值提供商或模型粘合剂?请显示解决我的问题的代码示例.
asp.net-mvc custom-model-binder value-provider asp.net-mvc-3
我们有一些PHP和Javascript应用程序调用一些ASP.NET MVC端点.假设我们有这个端点:
public ActionResult DoSomething(bool flag)
{
}
Run Code Online (Sandbox Code Playgroud)
我希望它匹配flag的值,无论我传入1或0的整数,还是传入一个"true"或"false"的字符串.我需要实现框架的哪个部分才能与之匹配?
asp.net-mvc model-binding value-provider asp.net-mvc-3 asp.net-mvc-4
我希望我的模型绑定不区分大小写。
我尝试操作从 继承的自定义模型绑定器System.web.Mvc.DefaultModelBinder,但我不知道在哪里添加不区分大小写的内容。
我也看了看IValueProvider,但我不想重新发明轮子并自己找到值。
任何的想法 ?
asp.net-mvc model-binding custom-model-binder value-provider asp.net-mvc-4
我遇到了一个我有默认MVC路由设置的场景.像所以.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
然后导航到这样的URL
domain/controller/action/1234
Run Code Online (Sandbox Code Playgroud)
在这个页面上,我然后导航到同一页面,但在某个事件之后使用不同的参数.像这样.
var id = $(this).data("id");
var url = "@Url.Action("action", "controller", new { area = ""})";
var params = $.param({id: id, vrnsearch: true});
var fullUrl = url += "?" + params;
window.location.href = fullUrl;
Run Code Online (Sandbox Code Playgroud)
我注意到url仍然在url中保持相同的id并附加参数.
domain/controller/action/1234?id=4321&vrnsearch=true
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,有没有办法确定优先权是否应该使用url或参数中的id值.
我实际上通过使用下面的方法找到了解决我的问题的方法,它从网址中删除了id并只使用了参数.
@Url.Action("action","controller", new {id = "", area = ""})
Run Code Online (Sandbox Code Playgroud)
但是,如果参数vs url路由中有优先权,那就太好奇了.