modelbinders

ASP.NET MVC - Custom Model Binder for ID fields

与世无争的帅哥 提交于 2019-12-24 00:44:39
问题 i have the following entities: public class Category { public virtual int CategoryID { get; set; } [Required(ErrorMessage = "Section is required")] public virtual Section Section { get; set; } [Required(ErrorMessage = "Category Name is required")] public virtual string CategoryName { get; set; } } public class Section { public virtual int SectionID { get; set; } public virtual string SectionName { get; set; } } Now within my add category view i have a textbox to enter the SectionID eg: <%=

ASP.NET MVC model binder parse decimal differently with GET and POST requests

主宰稳场 提交于 2019-12-24 00:33:20
问题 The server is hosting Asp.net mvc3 app and the Browser culture is set to da (Danish) GET request url: /get?d=1.1 (note that the decimal separator is .) return: da;1,1 (note that the decimal separator is ,) GET request url: /get?d=1,1 (the decimal separator is ,) return: Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'd' of non-nullable type 'System.Decimal' for method 'System.Web.Mvc.ContentResult get(System.Decimal)' in 'Intranet

Is binding domain models directly a bad idea?

 ̄綄美尐妖づ 提交于 2019-12-23 02:46:09
问题 I'm curious if binding models directly as parameters in the action method is considered as bad idea ? If the form gets to complex, I could create a custom model binder. Are there any pitfals using this approach ? I'd like to avoid creating of viewmodel, because I want to keep me app as simple as possible. I'd like to avoid of duplicating the code and the modelview to model binding. 回答1: I'd advise to almost always use view models. If you're using the default object templates... they don't

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

时光怂恿深爱的人放手 提交于 2019-12-22 11:35:32
问题 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,

MVC2 Modelbinder for List of derived objects

故事扮演 提交于 2019-12-22 10:49:03
问题 I want a list of different (derived) object types working with the Default Modelbinder in Asp.net MVC 2. I have the following ViewModel: public class ItemFormModel { [Required(ErrorMessage = "Required Field")] public string Name { get; set; } public string Description { get; set; } [ScaffoldColumn(true)] //public List<Core.Object> Objects { get; set; } public ArrayList Objects { get; set; } } And the list contains objects of diffent derived types, e.g. public class TextObject : Core.Object {

MVC3 ModelBinding to a collection posted back with index gaps

好久不见. 提交于 2019-12-19 08:27:49
问题 I have a collection of objects on my Model that I'm rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering each object. @Html.EditorFor(model => model.MyObjects) This has worked well for a while now, and when you check the html, my text boxes are prefixed with the model property, followed by the index of the collection they came from. <input class="text-box single-line" id="MyObjects_2__SomeProperty" name="MyObjects[2]

Modelbinding for empty query string parameters in ASP.NET MVC 2

回眸只為那壹抹淺笑 提交于 2019-12-18 18:52:48
问题 The behavior described here appears to now be the default for ASP.NET MVC 2 (at least for Preview 1). When modelbinding a querystring like this : ?Foo=&Bar=cat The following binding occurs (assuming you're binding to a model with 'Foo' and 'Bar' string properties) ASP.NET MVC 1 model.Foo = ""; model.Bar = "cat": ASP.NET MVC 2 (preview 1 through RC) model.Foo = null; model.Bar = "cat": Wanted to give anyone who is playing with V2 a heads up since this wasn't mentioned in the 'gu-notes'. Also

ASP.net MVC v2 - Debugging Model Binding Issues - BUG?

荒凉一梦 提交于 2019-12-18 11:45:57
问题 I am having more than a little difficulty trying to debug why MVC is not binding correctly in a given case I have... Basically, I have my action which receives a complex object which in turn has a complex child object - Activity.Location.State (Where Activity is the complex object that the action expects, Location is a complex child object and State is just a string). Now I set up a test project which as far as I can tell exactly mimics the actually scenario I have, in this test case the

Setting ModelState values in custom model binder

大憨熊 提交于 2019-12-18 02:27:12
问题 I am using custom model binder in ASP.NET MVC 2 that looks like this: public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } BaseContentObject obj = (BaseContentObject)base.BindModel(controllerContext, bindingContext); if(string.IsNullOrWhiteSpace(obj.Slug)) { // creating

Why does ASP.NET MVC care about my read only properties during databinding?

爱⌒轻易说出口 提交于 2019-12-17 16:05:08
问题 Edit: Added bounty because I'm seeking an MVC3 solution (if one exists) other than this: DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; I have a read only property on my 'Address' model 'CityStateZip' . It's just a convenient way to get city, state, zip from a US address. It throws an exception if the country is not USA (the caller is supposed to check first). public string CityStateZip { get { if (IsUSA == false) { throw new ApplicationException(