defaultmodelbinder

How to set DefaultBinder ModelBinders.Binders.DefaultBinder in asp.net mvc 4 using autofac?

偶尔善良 提交于 2020-02-05 05:39:11
问题 I want to set ModelBinders.Binders.DefaultBinder=new SmartBinder() in asp.net mvc 4 using autofac, what is the right way to do that? 回答1: In your Global.asax.cs; protected void Application_Start() { System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new SmartBinder(); } Autofac is not needed to do that. 来源: https://stackoverflow.com/questions/18286948/how-to-set-defaultbinder-modelbinders-binders-defaultbinder-in-asp-net-mvc-4-usi

How to send a list of int with jQuery to ASP.net MVC Default Model Binder

耗尽温柔 提交于 2020-01-09 19:22:10
问题 When I send a list of int's with jQuery like this: $.ajax('@Url.Action("Execute")', { type: 'POST', data: { pkList: [1,2,3] } }); Then jQuery will transform the pkList object and send it by post like this: pkList[]:1 pkList[]:2 pkList[]:3 Which would be fine if the server is PHP but I use Asp.NET MVC3 and try to get these values with the default model binder: public ActionResult Execute(ICollection<int> pkList) But pkList is always null, it seems that the default model binder cannot bind it.

Formatting nullable DateTime fields in strong typed View

不问归期 提交于 2020-01-01 18:17:18
问题 I have a Person class with a BornDate property in my model defined as [DisplayName("Born Date")] public DateTime? BornDate { get; set; } I use this field in my view as <td style="white-space:nowrap"> <%= Html.LabelFor(model => model.BornDate)%> <br /> <%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%> <%= Html.ValidationMessageFor(model => model.BornDate, "*")%> </td> The problem is that when I am editing a Person instance with the BornDate text box is formatted as dd/MM

ASP.Net MVC3 Parent Child Model Binding

僤鯓⒐⒋嵵緔 提交于 2019-12-24 09:56:12
问题 I have a partial template that uses a User object as a model. The user has a collection of Accounts. On this partial template I have a loop as follows. The _Account partial template is bound to the Account class @foreach (var item in Model.Accounts) { <tr> <td colspan="6"> <div> @Html.Partial("_Account", item) </div> </td> </tr> } In my controller method I initially tried [AcceptVerbs(HttpVerbs.Post)] public ActionResult UserDetails(User user, string actionType) But the User.Accounts

ASP.NET MVC Model Binder not working with a dictionary

穿精又带淫゛_ 提交于 2019-12-24 07:54:47
问题 Given the following view model and action using the DefaultModelBinder , it seems to ignore the dictionary, but bind all other properties correctly. Am I missing something here? Looking at the MVC source code this seems legit. Thanks public class SomeViewModel { public SomeViewModel() { SomeDictionary = new Dictionary<string, object>(); } public string SomeString { get; set; } public IDictionary<string, object> SomeDictionary { get; set; } } [HttpPost] public ActionResult MyAction

MS MVC3 Model Binding an Object

被刻印的时光 ゝ 提交于 2019-12-24 01:48:22
问题 Can someone help me better understand the DefaultModelBinder and how it handles binding a Model that has a property of type object? I've downloaded the code and tried tracing through it, but am still scratching my head a little. Let's say I have a Model like this: public class MyModel{ public object MyProperty{ get; set; } } And assume that my forms are all generated correctly (ex: name="MyModel.MyProperty") What happens for various cases of MyProperty actually being set to instances of

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 {

ASP.NET MVC ModelBinder bug: Binding request variable “id” to Model's property “Id”

强颜欢笑 提交于 2019-12-22 01:34:25
问题 I am using MVC 3 final RTM. Given This route: context.MapRoute( "Blog_Posts", "Blog/Posts/{id}/{slug}", new { controller = "Posts", action = "Index", slug = UrlParameter.Optional } ); And on a post's page, e.g. /blog/posts/2/some-slug I bind a partial view with a Comment model: @Html.Partial("_CommentEditor", new Comment()) And Comment has a public int Id {get; set;} . And in the partial view, I have this: @Html.HiddenFor(comment => comment.Id) Why does it display this? <input type="hidden"

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]

Is there a reason why the default modelbinder doesn't bind to fields?

谁说我不能喝 提交于 2019-12-18 08:29:07
问题 I'm using ASP.NET MVC3 and i'm wondering that the default modelbinder binds to public properties but not to public fields. Normally i just define the model classes with properties but sometimes i use some predefined classes which contains some fields. And everytime i have to debug and remember that the modelbinder just don't like fields. The question: Whats the reason behind it? 回答1: but sometimes i use some predefined classes which contains some fields While I cannot answer your question