model-binding

How Do I Model Bind A List Of 'List<SelectItem>' Using MVC.Net

為{幸葍}努か 提交于 2019-12-01 21:49:17
I am trying to create a form that will consist of a series of dropdown lists, all of which are loaded from a database. I will not know how many dropdown lists will be needed, or how many options each dropdown list will have at compile-time. How can these fields be set-up to allow them to model-bind when posted? There is a lot of other complexity in each of the below code elements, but I cannot get the model binding to work even when reduced down to a basic level. The Models: public class MyPageViewModel { public List<MyDropDownListModel> ListOfDropDownLists { get; set; } } public class

Model Binding for multipart/form-data (File + JSON) post in ASP.NET Core 1.1

蹲街弑〆低调 提交于 2019-12-01 17:11:11
I'm attempting to build an ASP.NET Core 1.1 Controller method to handle an HTTP Request that looks like the following: POST https://localhost/api/data/upload HTTP/1.1 Content-Type: multipart/form-data; boundary=--------------------------625450203542273177701444 Host: localhost Content-Length: 474 ----------------------------625450203542273177701444 Content-Disposition: form-data; name="file"; filename="myfile.txt" Content-Type: text/plain << Contents of my file >> ----------------------------625450203542273177701444 Content-Disposition: form-data; name="text" Content-Type: application/json {

Why BindNever attribute doesn't work

旧城冷巷雨未停 提交于 2019-12-01 16:00:46
问题 I do not want do bind the Id property on my CustomerViewModel so I added a [BindNever] attribute but it is not working. What could be the solution? I have the following: CustomerController.cs // PUT api/customers/5 [HttpPut("{id}")] public async Task<IActionResult> Put([FromUri] int id, [FromBody]CustomerViewModel customer) { //Implementation } CustomerViewModel public class CustomerViewModel { [BindNever] public int Id { get; set; } public string LastName { get; set; } public string

ModelBinder validation breaks on getter using reflection

不问归期 提交于 2019-12-01 14:15:31
I've ran across a problem which seems to be related to reflection and model binder validation, and FormatterParameterBinding.ExecuteBindingAsync(..) in particular, and though I can use a Method to do what I want I would prefer it if I could use a Property. Here I'm looking for some insight into the model binder validation process, why I am unable to do what I want and how I can fix the problem, or get around it. The setup public class ModelBindingValidationBreaker { public ModelBindingValidationBreaker() { Properties = new List<string>(); } public int A { get; set; } public int B { get; set; }

Is model binding possible without mvc?

坚强是说给别人听的谎言 提交于 2019-12-01 13:31:28
Say I have a Dictionary<string, string> and I want to update an object with the values from the dictionary, just like model binding in MVC... how would you do that without MVC? You could use the DefaultModelBinder to achieve this but you will need to reference the System.Web.Mvc assembly to your project. Here's an example: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Linq; using System.Web.Mvc; public class MyViewModel { [Required] public string Foo { get; set; } public Bar Bar { get; set; } } public class

Apply Custom Model Binder to Object Property in asp.net core

ε祈祈猫儿з 提交于 2019-12-01 10:59:13
I am trying to apply custom model binder for DateTime type property of model. Here is the IModelBinder and IModelBinderProvider implementations. public class DateTimeModelBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Metadata.ModelType == typeof(DateTime)) { return new BinderTypeModelBinder(typeof(DateTime)); } return null; } } public class DateTimeModelBinder : IModelBinder { private string[] _formats = new string[] { "yyyyMMdd", "yyyy-MM-dd",

ModelBinder validation breaks on getter using reflection

我与影子孤独终老i 提交于 2019-12-01 10:08:25
问题 I've ran across a problem which seems to be related to reflection and model binder validation, and FormatterParameterBinding.ExecuteBindingAsync(..) in particular, and though I can use a Method to do what I want I would prefer it if I could use a Property. Here I'm looking for some insight into the model binder validation process, why I am unable to do what I want and how I can fix the problem, or get around it. The setup public class ModelBindingValidationBreaker { public

How to discern between model binding errors and model validation errors?

血红的双手。 提交于 2019-12-01 09:31:45
I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values). As an example, given an action like [HttpPut("{id}")] public async Task<IActionResult> UpdateAsync( [FromRoute] int id, [FromBody] ThingModel model) I'd like to return a 400 status code when the id parameter in the route contains a non-digit character or when no body has been specified in the

How to discern between model binding errors and model validation errors?

倖福魔咒の 提交于 2019-12-01 06:37:57
问题 I'm implementing a REST API project using ASP.NET Core MVC 2.0, and I'd like to return a 400 status code if model binding failed (because the request is syntactically wrong) and a 422 status code if model validation failed (because the request is syntactically correct but contains unacceptable values). As an example, given an action like [HttpPut("{id}")] public async Task<IActionResult> UpdateAsync( [FromRoute] int id, [FromBody] ThingModel model) I'd like to return a 400 status code when

MVC Custom type property is not binding in my model

纵饮孤独 提交于 2019-12-01 06:33:32
I have the following scenario: A C# base project, with all data domains (custom types) that are used by all others projects in the company. So, It´s a little bit hard to modify it. Now, we are creating our first mvc project with that base project as reference, and the model binding doesnt work for properties of those strings custom types: [Serializable] [TypeConverter(typeof(ShortStrOraTypeConverter))] public class ShortStrOra : BaseString { public ShortStrOra() : this(String.Empty) { } public ShortStrOra(string stringValue) : base(stringValue, 35) { } public static implicit operator