我的应用程序有许多模型,其中许多包含百分比数据.这些在模型中表示为decimal或decimal?结构.但是,并非所有具有decimal结构的属性都是百分比.有些应该像常规小数一样对待.
百分比需要特别注意:
{0:P2}格式.(我有这部分工作.)我开始走上创建之路PercentModelBinder实现IModelBinder,但后来意识到,你只能应用ModelBinderAttribute到类,而不是一个属性.
处理这种情况的最佳方法是什么,某种类型的(但不是全部)使用需要对显示和绑定进行特殊处理?
我认为每个解决方案都有过分杀戮,与MVC框架作斗争.当然有一种比以下更好的方法:
Percentage结构并将其用作IModelBinder和EditorTemplates的基础,或decimal并decimal?根据对模型的深入了解来改变解析逻辑,或者正如我从下面的问题中理解的那样,应该可以为Get和Post操作使用不同的模型.但不知何故,我未能实现这一目标.
我错过了什么?
相关问题:在控制器操作中使用两个不同的模型进行POST和GET
模型
public class GetModel
{
public string FullName;
public string Name;
public int Id;
}
public class PostModel
{
public string Name;
public int Id;
}
Run Code Online (Sandbox Code Playgroud)
调节器
public class HomeController : Controller
{
public ActionResult Edit()
{
return View(new GetModel {Id = 12, Name = "Olson", FullName = "Peggy Olson"});
}
[HttpPost]
public ActionResult Edit(PostModel postModel)
{
if(postModel.Name == null)
throw new Exception("PostModel was not filled correct");
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
视图
@model MvcApplication1.Models.GetModel
@using (Html.BeginForm()) …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在控制器中执行动作后解析模型的方法,描述问题的最简单方法是:
public DTO[] Get(string filterName)
{
//How can I do this
this.Resolve<MyCustomType>("MyParamName");
}
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找有关我为什么要这样做的更多信息,则可以继续阅读以获取完整信息。
TL; DR
我正在寻找一种解决模型请求的方法,给定一个始终从查询字符串中解析的参数名称,该如何从启动中动态注册过滤器。我有一个要处理注册过滤器的类。
在启动类中,我希望能够向我的restServices动态注册过滤器。我有一个用来传递给我的自定义ControllerFeatureProvider的选项,大致如下所示:
public class DynamicControllerOptions<TEntity, TDTO>
{
Dictionary<string, Func<HttpContext, Expression<Func<TEntity, bool>>>> _funcNameToEndpointResolverMap
= new Dictionary<string, Func<HttpContext, Expression<Func<TEntity, bool>>>>();
Dictionary<string, List<ParameterOptions>> _filterParamsMap = new Dictionary<string, List<ParameterOptions>>();
public void AddFilter(string filterName, Expression<Func<TEntity, bool>> filter)
{
this._funcNameToEndpointResolverMap.Add(filterName, (httpContext) => filter);
}
public void AddFilter<T1>(string filterName, Func<T1, Expression<Func<TEntity, bool>>> filterResolver,
string param1Name = "param1")
{
var parameters = new List<ParameterOptions> { new ParameterOptions { Name = param1Name, Type …Run Code Online (Sandbox Code Playgroud) 我不确定这是否是DefaultModelBinder类中的错误或者是什么.但UpdateModel通常不会更改模型的任何值,除了它找到匹配的值.看看以下内容:
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Edit(List<int> Ids)
{
// Load list of persons from the database
List<Person> people = GetFromDatabase(Ids);
// shouldn't this update only the Name & Age properties of each Person object
// in the collection and leave the rest of the properties (e.g. Id, Address)
// with their original value (whatever they were when retrieved from the db)
UpdateModel(people, "myPersonPrefix", new string[] { "Name", "Age" });
// ...
}
Run Code Online (Sandbox Code Playgroud)
会发生什么是UpdateModel创建新的 Person对象,从ValueProvider分配它们的Name和Age属性并将它们放在参数List <>中,这使得其余的属性设置为它们的默认初始值(例如Id = 0),那么什么是去吧?
假设我有一个User实体,我想在构造函数中将它的CreationTime属性设置为DateTime.Now.但作为单元测试采用者,我不想直接访问DateTime.Now,而是使用ITimeProvider:
public class User {
public User(ITimeProvider timeProvider) {
// ...
this.CreationTime = timeProvider.Now;
}
// .....
}
public interface ITimeProvider {
public DateTime Now { get; }
}
public class TimeProvider : ITimeProvider {
public DateTime Now { get { return DateTime.Now; } }
}
Run Code Online (Sandbox Code Playgroud)
我在我的ASP.NET MVC 2.0应用程序中使用NInject 2.我有一个UserController和两个Create方法(一个用于GET,一个用于POST).用于GET的那个是直接的,但是用于POST的那个不是那么直接而不是那么向前:P因为我需要弄乱模型绑定器告诉它获得ITimeProvider的实现的引用以便能够构造用户实例.
public class UserController : Controller {
[HttpGet]
public ViewResult Create() {
return View();
}
[HttpPost]
public ActionResult Create(User user) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我还希望能够保留默认模型绑定器的所有功能.
有机会解决这个简单/优雅/等?:d
asp.net-mvc dependency-injection inversion-of-control model-binding custom-model-binder
有没有办法可以为单个对象调用模型绑定器?
我不想/需要自定义模型绑定器 - 我只想做这样的事情:
MyViewModel1 vModel1 = new MyViewModel1();
InvokeModelBinder(vModel1);
MyViewModel2 vModel2= new MyViewModel2();
InvokeModelBinder(vModel2);
Run Code Online (Sandbox Code Playgroud)
当我完成后,vModel1和vModel2的属性已经绑定到传入请求中的内容.由于我们的控制器/操作的编写方式,我不一定要在操作方法的输入列表中列出vModel1和vModel2(因为最终会有一个可能很长的视图模型列表,可以选择绑定).
asp.net mvc团队是否为枚举实现了默认模型绑定?一个开箱即用,无需为枚举创建自定义模型绑定器.
更新:
假设我有一个接收视图模型的动作,JSON对象将被发布到动作中.
jsObj{id:2, name:'mike', personType: 1}
Run Code Online (Sandbox Code Playgroud)
和视图模型:
class ViewModel
{
public int id {get;set;}
public string name {get;set;}
public PersonType personType{get;set;}
}
public enum PersonType : int
{
Good = 1,
Bad = 2,
Evil = 3
}
Run Code Online (Sandbox Code Playgroud)
这个人类型会受到约束吗?
我正在努力建立这个:

当我在左侧编辑字段时,它应该更新右边的字段,反之亦然.
在输入字段中编辑值会导致文本光标在其末尾跳转.
在华氏温度字段中键入"2"将替换为1.999999999999,如屏幕截图所示.这是因为双重转换:
视图的Fº→模型的Cº→视图的Fº.

我怎么能避免这种情况?
更新:
我想知道在Backbone.js等MVC框架中处理双向绑定的优雅方式.
var Temperature = Backbone.Model.extend({
defaults: {
celsius: 0
},
fahrenheit: function(value) {
if (typeof value == 'undefined') {
return this.c2f(this.get('celsius'));
}
value = parseFloat(value);
this.set('celsius', this.f2c(value));
},
c2f: function(c) {
return 9/5 * c + 32;
},
f2c: function(f) {
return 5/9 * (f - 32);
}
});
var TemperatureView = Backbone.View.extend({
el: document.body,
model: new Temperature(),
events: {
"input #celsius": "updateCelsius",
"input #fahrenheit": "updateFahrenheit"
},
initialize: function() {
this.listenTo(this.model, …Run Code Online (Sandbox Code Playgroud) data-binding model-view-controller model-binding backbone.js
在Nancy中,有没有办法将POST请求的内容绑定到动态类型?
例如:.
// sample POST data: { "Name": "TestName", "Value": "TestValue" }
// model class
public class MyClass {
public string Name { get; set; }
public string Value { get; set; }
}
// NancyFx POST url
Post["/apiurl"] = p => {
// this binding works just fine
var stronglyTypedModel = this.Bind<MyClass>();
// the following bindings do not work
// there are no 'Name' or 'Value' properties on the resulting object
dynamic dynamicModel1 = this.Bind();
var dynamicModel2 = this.Bind<dynamic>(); …Run Code Online (Sandbox Code Playgroud) 我有一个导航栏,有几个链接,如下所示:
<a href="MyController/Browse/2">MenuItem1</a>
这个请求会触及我的动作方法:
public ActionResult Browse(int departmentId)
{
var complexVM = MyCache.GetComplexVM(departmentId);
return View(complexVM);
}
Run Code Online (Sandbox Code Playgroud)
这是我的ComplexVM:
public class ComplexVM
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MyCache,是一个静态的部门列表,我保留在内存中,所以当用户传入时DepartmentId,我不需要DepartmentName从DB 获取相应的.
这工作正常...但如果我能以某种方式初始化ComplexVM自定义模型绑定器,而不是在Controller中初始化它会很好...所以我仍然想要使用链接(菜单项),但这一次,CustomModelBinder将我的参数2绑定到ComplexVM:它需要从id MyCache和2中查找部门的名称并初始化ComplexVM,然后ComplexVM将传递给此操作方法:
public ActionResult Browse(ComplexVM complexVM)
{
return View(complexVM);
}
Run Code Online (Sandbox Code Playgroud)
我想在没有回复的情况下点击上面的控制器,因为我的导航栏中有很多菜单项链接...不确定这是否可行?或者,如果这是一个好主意?
我已经看到了这个链接,它描述了我想要的东西......但我不确定路由是如何工作的......即routing id:2=>ComplexVM
或者可以这样做RouteConfig,像这样:
routes.MapRoute(
name: "Browse",
url: …Run Code Online (Sandbox Code Playgroud) model-binding ×10
asp.net-mvc ×5
.net ×2
c# ×2
.net-core ×1
backbone.js ×1
data-binding ×1
nancy ×1
rest ×1
updatemodel ×1
viewmodel ×1