model-binding

ASP.NET MVC 2.0 - IList<T> CheckBoxes

寵の児 提交于 2020-01-15 08:40:34
问题 What is the best way to handle this: class Option { int id; string name; } class QuoteItem { IList<Option> options; } class QuoteViewModel { IList<Option> allOptions; QuoteItem quoteItem; } Basically, I have all the available options in allOptions . I want to have a checkbox that puts another Option (even if its just its id ) into the QuoteItem.options list when it is checked. How would I accomplish this? Would it best be an IList<bool> and bind it after the fact? 回答1: I suggest you take look

Asp.net Core model binder accepts random integer for boolean types

淺唱寂寞╮ 提交于 2020-01-15 01:24:33
问题 Given the model has a boolean property: public class Person { public string Name { get; set; } public bool IsMale { get; set; } } When trying to POST the following payload: { "name": "Bob", "isMale": 12345 // any random integer } To a simple action: [HttpPost] public IActionResult Post([FromBody] Person person) { if (ModelState.IsValid) return Ok(); return BadRequest(ModelState); } The person.IsMale property gets the value of true . If passing isMale: "foobar" i get an invalid type error If

Asp.net Core model binder accepts random integer for boolean types

末鹿安然 提交于 2020-01-15 01:24:05
问题 Given the model has a boolean property: public class Person { public string Name { get; set; } public bool IsMale { get; set; } } When trying to POST the following payload: { "name": "Bob", "isMale": 12345 // any random integer } To a simple action: [HttpPost] public IActionResult Post([FromBody] Person person) { if (ModelState.IsValid) return Ok(); return BadRequest(ModelState); } The person.IsMale property gets the value of true . If passing isMale: "foobar" i get an invalid type error If

MVC Web API Binding Model to a Derived Class

本秂侑毒 提交于 2020-01-13 14:29:02
问题 I am looking at how to bind a model to a derived class within MVC Web API, the issue I have is that I think I have found about 5 ways of doing it... What I have is: Models -> ModelBase ModelA : ModelBase ModelB : ModelBase Controller then containers the method: Post(ModelBase model) {} The data that is posted will be ModelA or ModelB, I want to add information to the HTTP Request metadata (think Content-Type: application/json; type=ModelA) and based on this tell MVC to bind the posted content

.NET Core Custom model binder call default model binder

无人久伴 提交于 2020-01-13 08:57:28
问题 I'm trying to implement some middleware in .NET Core that rounds decimals to 2 decimal places. All of the other mapping can work as it currently does via the ComplexTypeModelBinder . I've tried calling that binder before mine or inheriting from it, but it ends up with the model just being null when it hits the controller. Essentially I'm after the same functionality as asked here: Call Default Model Binder from a Custom Model Binder?, but for .NET core. 回答1: public enum OPERATION { none, add,

How to do Model Binding with Jquery Ajax

送分小仙女□ 提交于 2020-01-13 03:15:33
问题 I'd like to use model binding to keep my controllers looking cleaner, you can see how much nicer it is using model binding: public ActionResult Create(Person personToCreate) { //Create person here } vs public ActionResult Create(string firstName, string lastName, string address, string phoneNum, string email, string postalCode, string city, string province, string country) { //Create person here } When doing model binding, we can just use a form with the correct names in the Html.TextBox("")

Binding abstract action parameters in WebAPI

痞子三分冷 提交于 2020-01-12 07:28:08
问题 I'm in a situation where I need to bind an incoming HTTP POST request with data in the body, to a concrete type depending on a ProductType denominator in the data. Here is my Web API 2 action method: [HttpPost, Route] public HttpResponseMessage New(ProductBase product) { // Access concrete product class... if (product is ConcreteProduct) // Do something else if (product is OtherConcreteProduct) // Do something else } I was first thinking of using a custom model binder, but it seems like it

Nancy model binding to child class

纵饮孤独 提交于 2020-01-12 05:28:27
问题 We are having an issue with Nancy's default model binder. Given the below... public class Foo { public Foo() { } public string Name { get; set; } public Bar Bar { get; set; } } public class Bar { public string Name { get; set; } } with elements like... <input type="text" value="Name" /> <input type="text" value="Bar.Name" /> With the default model binder used like so.. var foo = this.Bind<Foo>(); This correctly binds Foo.Name but fails to bind Foo.Bar.Name Is there a way to enable this kind

MVC 3 doesn't bind nullable long

南笙酒味 提交于 2020-01-11 19:52:06
问题 I made a test website to debug an issue I'm having, and it appears that either I'm passing in the JSON data wrong or MVC just can't bind nullable longs. I'm using the latest MVC 3 release, of course. public class GetDataModel { public string TestString { get; set; } public long? TestLong { get; set; } public int? TestInt { get; set; } } [HttpPost] public ActionResult GetData(GetDataModel model) { // Do stuff } I'm posting a JSON string with the correct JSON content type: { "TestString":"test"

MVC 3 doesn't bind nullable long

那年仲夏 提交于 2020-01-11 19:50:12
问题 I made a test website to debug an issue I'm having, and it appears that either I'm passing in the JSON data wrong or MVC just can't bind nullable longs. I'm using the latest MVC 3 release, of course. public class GetDataModel { public string TestString { get; set; } public long? TestLong { get; set; } public int? TestInt { get; set; } } [HttpPost] public ActionResult GetData(GetDataModel model) { // Do stuff } I'm posting a JSON string with the correct JSON content type: { "TestString":"test"