defaultmodelbinder

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

大城市里の小女人 提交于 2019-12-06 09:39: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

DefaultModelBinder cannot deserialize .NET Dictionary object passed to an action as a JSON object?

ぃ、小莉子 提交于 2019-12-05 06:31:33
I have a very simple class: public class FilterItem { public Dictionary<string, string> ItemsDictionary { get; set; } public FilterItem() { ItemsDictionary = new Dictionary<string, string>(); } } I want to populate the data in the dictionary on the client and then pass it to my controller action as a JSON object. However no matter what I try on the client, the DefaultModelBinder does not seem to be able to deserialize it. Here is an example javascript code to call my action: var simpleDictionary = {"ItemsDictionary": {"1": "5", "2": "7"}}; $.ajax({ cache: false, type: "POST", data: JSON

.NET core custom and default binding combined

柔情痞子 提交于 2019-12-05 05:59:37
I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof

Formatting nullable DateTime fields in strong typed View

梦想与她 提交于 2019-12-04 17:47:22
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/yyyy hh.mm.ss while I would like to format it without the time part ("dd/MM/yyyy"). I am not able to

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

ASP.NET MVC DefaultModelBinder with nested lists

最后都变了- 提交于 2019-12-04 00:22:24
I have a View with a table representing an employee's timesheet. Days across the top, projects down the side, with each day/project intersection containing two values for regular hours and overtime. The (simplified) class definitions for the page model are: public class TimesheetFormModel { public List<Project> Projects; // other things... } public class Project { public string Name; public List<WorkUnit> WorkUnits; } public class WorkUnit { public DateTime Date; public decimal RegularHours; public decimal OvertimeHours; } The form elements on the page are named as follows in an attempt to get

MVC3 ModelBinding to a collection posted back with index gaps

*爱你&永不变心* 提交于 2019-12-01 06:20:36
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].SomeProperty" type="Text" value="" /> However I've recently started using the ShowForEdit and ShowForDisplay

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

落花浮王杯 提交于 2019-11-29 14:31:57
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? but sometimes i use some predefined classes which contains some fields While I cannot answer your question about the exact reason why the default model binder works only with properties (my guess is that it respects

bind attribute include and exclude property with complex type nested objects

痞子三分冷 提交于 2019-11-27 13:59:47
问题 Ok, this is weird. I cannot use BindAttribute 's Include and Exclude properties with complex type nested objects on ASP.NET MVC. Here is what I did: Model: public class FooViewModel { public Enquiry Enquiry { get; set; } } public class Enquiry { public int EnquiryId { get; set; } public string Latitude { get; set; } } HTTP POST action: [ActionName("Foo"), HttpPost] public ActionResult Foo_post( [Bind(Include = "Enquiry.EnquiryId")] FooViewModel foo) { return View(foo); } View: @using (Html

MVC3 Non-Sequential Indices and DefaultModelBinder

烈酒焚心 提交于 2019-11-26 15:56:12
Is it true that the default model binder in MVC 3.0 is capable of handling non-sequential indices (for both simple and complex model types)? I've come across posts that suggest it should, however in my tests it appears that it does NOT. Given post back values: items[0].Id = 10 items[0].Name = "Some Item" items[1].Id = 3 items[1].Name = "Some Item" items[4].Id = 6 items[4].Name = "Some Item" And a controller method: public ActionResult(IList<MyItem> items) { ... } The only values that are loaded are items 0 and 1; item 4 is simply ignored. I've seen numerous solutions to generate custom indices