model-binding

string.Format in Data Annotation Validation Attributes

∥☆過路亽.° 提交于 2020-01-04 06:56:31
问题 Is there any way to set Data Annotation messages using formatted string instead of direct constant, I need to set required field ErrorMessage like in following code, but it gives me an error. [Required(ErrorMessage = string.Format(SystemMessages.Required, "First Name"))] public string FirstName { get; set; } SystemMessages is a constant file and it has following code, public const string Required = "{0} is required."; Is there any way to set attributes like this? 回答1: string.Format result is

string.Format in Data Annotation Validation Attributes

眉间皱痕 提交于 2020-01-04 06:56:31
问题 Is there any way to set Data Annotation messages using formatted string instead of direct constant, I need to set required field ErrorMessage like in following code, but it gives me an error. [Required(ErrorMessage = string.Format(SystemMessages.Required, "First Name"))] public string FirstName { get; set; } SystemMessages is a constant file and it has following code, public const string Required = "{0} is required."; Is there any way to set attributes like this? 回答1: string.Format result is

Override Message “the value {0} is invalid for {1}” in case of int in WebAPI

耗尽温柔 提交于 2020-01-04 06:12:10
问题 I have a variable name CountryId (Integer Type). If user provides a string or any random input to CountryId , the In-built DefaultBindingModel in ASP.Net throws an error : The value '<script>gghghg</script>' is not valid for CountryId. I want to override this message and provide my own text if the ModelState fails. I want a generic solution. I've searched and tried many solutions, but they only worked for MVC applications, not webAPI . public class IntegerModelBinder : DefaultModelBinder {

Override Message “the value {0} is invalid for {1}” in case of int in WebAPI

我与影子孤独终老i 提交于 2020-01-04 06:11:42
问题 I have a variable name CountryId (Integer Type). If user provides a string or any random input to CountryId , the In-built DefaultBindingModel in ASP.Net throws an error : The value '<script>gghghg</script>' is not valid for CountryId. I want to override this message and provide my own text if the ModelState fails. I want a generic solution. I've searched and tried many solutions, but they only worked for MVC applications, not webAPI . public class IntegerModelBinder : DefaultModelBinder {

ASP.NET Core Model Bind Collection of Unknown Length

六眼飞鱼酱① 提交于 2020-01-04 05:48:06
问题 I'm trying to work out how to model bind a collection to a model property within asp.net core. I understand that the way this can be done is to include fields as so: <input type="hidden" asp-for="items[0].Id" /> <input type="hidden" asp-for="items[0].Name" /> <input type="hidden" asp-for="items[1].Id" /> <input type="hidden" asp-for="items[1].Name" /> However what do you do if you want to allow the user to add additional items and/or remove others. In this case I am adding these items client

Mapping ViewModel to JS Object

痞子三分冷 提交于 2020-01-04 03:15:23
问题 Is there possible to map a model (server side) into JS object? The problem is that I can not return a json object from the server side. View.cshtml: @model TestModel <script type="text/javascript"> jQuery(function ($) { var jsObject = { Property1: @Model.Property1, Property2: @Model.Property2, Property3: @Model.Property3, ... }; }); </script> Thanks! 回答1: You can use Json.Encode Method converts a data object to a string that is in the JavaScript Object Notation (JSON) format. var jsObject =

MVC Date Time Model Binding

本小妞迷上赌 提交于 2020-01-04 02:46:12
问题 I am using 2 kendo date pickers in my application as such: <div class="span12"> <div class="span2" style="text-align: right"> Start Date: </div> <div class="span2"> @(Html.Kendo().DatePickerFor(m=>m.StartDate)) </div> <div class="span2" style="text-align: right"> End Date: </div> <div class="span2"> @(Html.Kendo().DatePickerFor(m=>m.EndDate)) </div> <div class="span4"> <button class="btn btn-primary" onclick="getGraphData()">Show</button> </div> </div> When the button is clicked, I read the

How to extend ComplexTypeModelBinder

﹥>﹥吖頭↗ 提交于 2020-01-03 20:59:28
问题 I need to customize model binding for a particular class Foo that involves extending the normal binding logic with some additional post-processing (e.g., conditionally set null collection fields to an empty collection). I want to add this logic to model binding so that the results are available to action filters, etc. The most direct approach would be to derive from ComplexTypeModelBinder and override BindModelAsync . However, that method is unfortunately not virtual. Composition is the next

How do I use custom model binder that supports dependency injection in ASP.NET Core?

人盡茶涼 提交于 2020-01-03 13:39:15
问题 I am trying to use a custom model binder in MVC that I want resolved from my IoC container. The issue I am having is that I can't access my container while I am adding the MVC service, because my container isn't built yet (and I need to add MVC before building my container). Feels like a chicken/egg issue, and I am sure I am missing a simple solution. Example: services.AddMvc().AddMvcOptions(options => { options.ModelBinders.Add(serviceProvider.Resolve<CustomModelBinder>()); }); My custom

Radiobutton, checkbox and select elements don't get populated accordingly

∥☆過路亽.° 提交于 2020-01-03 06:19:25
问题 I have different kinds of elements - textboxes, selects, checkboxes and radio buttons on my view. When I present this strongly typed view with the model there are some elements that get populated with corresponding model values but there are certain kinds and ones that don't. Radio buttons, checkboxes are blank. When it comes to select elements, if they are created with @Html.DropDownListFor helper method then they get correct values, but if they are just plain Html selects with their names