model-binding

MVC 4 ignores DefaultModelBinder.ResourceClassKey

自古美人都是妖i 提交于 2019-11-26 18:07:12
问题 Adding a resource file to App_GlobalResources with a PropertyValueRequired key and changing DefaultModelBinder.ResourceClassKey to the file name has no effect on MVC 4. The string The {0} field is required is never changed. I don't want to set the resource class type and key on every required field. Am I missing something? Edit: I've made a small modification on Darin Dimitrov's code to keep Required customizations working: public class MyRequiredAttributeAdapter : RequiredAttributeAdapter {

ASP.NET MVC 3 Model Binding Resources

依然范特西╮ 提交于 2019-11-26 17:32:55
问题 I am looking for a good resource that describes very thoroughly how model binding works with ASP.NET MVC 3 (or to a lesser extent, MVC 2) and different approaches. I have not been able to find any good resources on this topic, except bits and pieces there. The information on the net is more about "how to do X" than explaining how the internals of model binding work. Any recommendations? Book recommendations are fine as well. 回答1: Take a look: ASP.NET MVC Model Binding - Part1 and Part2 http:/

MVC 3 Model Binding a Sub Type (Abstract Class or Interface)

左心房为你撑大大i 提交于 2019-11-26 17:22:45
Say I have a Product model, the Product model has a property of ProductSubType (abstract) and we have two concrete implementations Shirt and Pants. Here is the source: public class Product { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public decimal? Price { get; set; } [Required] public int? ProductType { get; set; } public ProductTypeBase SubProduct { get; set; } } public abstract class ProductTypeBase { } public class Shirt : ProductTypeBase { [Required] public string Color { get; set; } public bool HasSleeves { get; set; } } public class Pants :

Binding HttpPostedFileBase using Ajax.BeginForm

家住魔仙堡 提交于 2019-11-26 17:05:39
问题 I have a form which binds a model and a file upload using the default binder for HttpPostedFileBase. This works fine when using Html.BeginForm(). However, I wanted to perform the same action using AJAX so I replaced this with Ajax.BeginForm() changing the parameters accordingly. The model still binds correctly, however I can't get the file upload to bind to the HttpPostedFileBase. This binds the model and the file upload: <% using (Html.BeginForm("MapUpdateColumns", "RepositoryAdmin",

ASP.NET MVC 4 JSON Binding to the View Model - Nested object error

╄→гoц情女王★ 提交于 2019-11-26 16:44:00
问题 I have got the problem with json binding to the view model. Here is my code: part of my ViewModels (AddressViewModel has more properties): public class AddressViewModel { [Display(Name = "Address_Town", ResourceType = typeof(Resources.PartyDetails))] public string Town { get; set; } [Display(Name = "Address_Country", ResourceType = typeof(Resources.PartyDetails))] public Country Country { get; set; } } public class Country : EntityBase<string> { public string Name { get; set; } protected

Passing UTC DateTime to Web API HttpGet Method results in local time

纵然是瞬间 提交于 2019-11-26 15:49:47
问题 I'm trying to pass a UTC date as a query string parameter to a Web API method. The URL looks like /api/order?endDate=2014-04-01T00:00:00Z&zoneId=4 The signature of the method looks like [HttpGet] public object Index(int zoneId, DateTime? endDate = null) The date is coming in as 31/03/2014 8:00:00 PM but I'd like it to come in as 01/04/2014 12:00:00 AM My JsonFormatter.SerializerSettings looks like this new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(

ASP.net MVC - Model binding excludes class fields?

人盡茶涼 提交于 2019-11-26 14:49:33
问题 In a recent project - i've hit an unexpected snag. A class with simple public fields (note not properties) doesn't seem to want to play nice with the ASP.net MVC 3.0 model binder. Is this by design? besides changing the fields to properties - any options here? update The reason for the simple fields (instead of properties) is because I'm working with a shared library between MVC and a Script Sharp project. Script sharp supports properties - but it becomes a mess with javascript in the view

Remote Validation for LIST of MODELs

∥☆過路亽.° 提交于 2019-11-26 14:44:04
I used the following tutorial: http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.aspx And everything seemed fine, but in my case, string Username always comes back null. After tonnes of research, I found everyone discovered BIND Prefixes. That would be great in many circumstances, but not this one. I should note all properties and names line up, however in my for loop, the EditorFor creates a [i].Username field and this doesn't map to any model property. QUESTION: I think I want to map [i].Username to Username where i is any number from 0-infinity, so when it GETS, the value is

Model binding with nested child models and PartialViews in ASP.NET MVC

喜夏-厌秋 提交于 2019-11-26 14:30:19
问题 I have the following types and classes: namespace MVC.Models public class Page { public EditableContent Content {get; set; } } public class EditableContent { public TemplateSection SidebarLeft {get; set; } public TemplateSection SidebarRight {get; set; } } I want to edit the Page instance in my Edit.aspx View. Because EditableContent is also attached to other models, I have a PartialView called ContentEditor.ascx that is strongly typed and takes an instance of EditableContent and renders it.

How does MVC 4 List Model Binding work?

人盡茶涼 提交于 2019-11-26 14:22:23
If I want a set of inputs in a form to bind to a List in MVC 4, I know that the following naming convention for input name attributes will work: <input name="[0].Id" type="text" /> <input name="[1].Id" type="text" /> <input name="[2].Id" type="text" /> But I am curious about how forgiving the model binder is. For example, what about the following: <input name="[0].Id" type="text" /> <input name="[3].Id" type="text" /> <input name="[8].Id" type="text" /> How would the model binder handle this? Would it bind to a List of length 9 with nulls? Or would it still bind to a List of length 3? Or would