modelbinders

Change default “The {0} field is required” (ultimate solution?)

て烟熏妆下的殇ゞ 提交于 2019-12-06 06:14:54
Good day! I've the following ViewModel class I use for login form: using System.ComponentModel.DataAnnotations; ... public class UserLogin : IDataErrorInfo { [Required] [DisplayName("Login")] public string Login { get; set; } [Required] [DisplayName("Password")] public string Password { get; set; } [DisplayName("Remember Me")] public bool RememberMe { get; set; } #region IDataErrorInfo Members // This will be a Model-level error public string Error { get { if (!WebUser.CanLogin(Login, Password)) { return Resources.ValidationErrors.InvalidLoginPassword; } else { return String.Empty; } } } //

If I need to retrieve an object from a custom model binder should the binder interact with the service layer, the repository layer, or …?

纵然是瞬间 提交于 2019-12-06 06:07:30
问题 If I have a class similar to this: public class Person { public string firstName { get; set; } public string lastName { get; set; } public Pet myPet { get; set; } } When I create a custom model binder, the Post from my form will not be sending in a Pet, it would send in data like this: firstName: "myFirstName" lastName: "myLastName" myPet: "myPetsName" Since the Pet's name is passed in, and not the actual Pet object, the Pet object needs to be retrieved from within the model binder. My

Extending Sanderson's custom mvc ModelBinder for an object stored in session

大兔子大兔子 提交于 2019-12-06 05:26:47
In his wonderful MVC book Steven Sanderson gives an example of a custom model binder that sets and retrieves a session variable, hiding the data storage element from the controller. I'm trying to extend this to cater for a pretty common scenario: I'm storing a User object in the session and making this available to every action method as a parameter. Sanderson's class worked ok when the User details weren't changing, but now i need to let the user edit their details and save the amended object back to the session. My problem is that I can't work out how to distinguish a GET from a POST other

asp.net MVC 1.0 and 2.0 currency model binding

荒凉一梦 提交于 2019-12-06 02:32:50
问题 I would like to create model binding functionality so a user can enter ',' '.' etc for currency values which bind to a double value of my ViewModel. I was able to do this in MVC 1.0 by creating a custom model binder, however since upgrading to MVC 2.0 this functionality no longer works. Does anyone have any ideas or better solutions for performing this functionality? A better solution would be to use some data annotation or custom attribute. public class MyViewModel { public double

Model Binding within a Model Binder

我与影子孤独终老i 提交于 2019-12-04 22:03:12
Firstly, bear with me here. I have a custom model binder which is successfully mapping form data to a custom object. Within this model binder it also maps form items to different custom object. What I feel I should be able to do is create a separate model binder to take care of this second mapping. This is a simplified version. Custom objects: public class Category { public int CategoryId { get; set; } public string Name { get; set; } public string Status { get; set; } public string Description { get; set; } public IEnumerable<SubCategory> SubCategories { get; set; } } public class SubCategory

ASP.NET MVC: avoid tight coupling when binding form POST to parameter

為{幸葍}努か 提交于 2019-12-04 16:54:36
Let's say I have an interface like: interface IThing { int Id { get; set; } string Title { get; set; } } And in ASP.NET MVC I have a form that posts to a controller action like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult NewThing([Bind(Exclude = "Id")] SimpleThing thing) { // code to validate and persist the thing can go here } Where SimpleThing is a concrete class that just barely implements IThing . However, I would like all my methods to deal with the interface. I have a data assembly that uses NHiberate and its own IThing implementation (let's call it RealThing ). I can't pass

How to use two instances of the same .ascx in the same page in ASP.NET MVC?

谁说我不能喝 提交于 2019-12-04 13:48:56
I have two instances of an Address.ascx control in an ASP.NET MVC page. <h1>Shipping Address</h1> <% Html.RenderPartial("Controls/AddressControl"); %> <h1>Billing Address</h1> <% Html.RenderPartial("Controls/AddressControl"); %> Of course, with the code exactly like this I'll end up with the same IDs for each field in the address. I can easily append a string to the ID of the fields so I'd have 'Street1_billing' and 'Street1_shipping' , but I'm not clear how to map this to the model. Whats the best solution to mapping a model to an array of items (in this case only 2). I'm not aware of any ASP

Retrieving data from view, should I use model binder?

ⅰ亾dé卋堺 提交于 2019-12-04 12:37:52
I am a bit lost here as I have not really looked at model binders so if possible, can one advise me if I am actually thinking about my problem correctly... :) and if my code is way of, please advise... 1 -I have a DTO class which contains 'custom fields' each with a name and other properties i.e.: Public Class CustomFields { public string Name {get;set;} public string Description {get;set;} public string FieldType {get;set;} public string Value {get;set;} } 2- Within my repo/business layer I am setting the values and returning ICollection for the view to render 3- the view uses a foreach to

Is it okay to hit the database from a custom model binder?

試著忘記壹切 提交于 2019-12-04 11:44:50
Say I have an object that gets some data from HttpPost and some from the database. I think I want to allow the ModelBinder to go to the database/repository for the that data missing from the post. In practice, is this a good or bad idea? I've decided to edit my original answer given my thinking on these types of things has evolved since early 2010. In my original answer, I basically expressed that, while my instincts told me you shouldn't do this, I was uncomfortable saying we shouldn't without being able to articulate why. Now, I'd recommend against this on the grounds that the responsibility

If I need to retrieve an object from a custom model binder should the binder interact with the service layer, the repository layer, or …?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 09:30:45
If I have a class similar to this: public class Person { public string firstName { get; set; } public string lastName { get; set; } public Pet myPet { get; set; } } When I create a custom model binder, the Post from my form will not be sending in a Pet, it would send in data like this: firstName: "myFirstName" lastName: "myLastName" myPet: "myPetsName" Since the Pet's name is passed in, and not the actual Pet object, the Pet object needs to be retrieved from within the model binder. My question is, should the model binder be interacting with the Service Layer, the Repository Layer, or should