model-binding

Return several checkbox values of Html.CheckBoxFor to the controller

梦想与她 提交于 2020-07-18 07:53:08
问题 I have a view where the user should select several listed items by checkboxes, which should then be worked within the controller. However, I am not getting the values passed to the POST method of the controller; the List comes in null when debugging. While searching for answers, I was not able to find one matching with C# and the Html.CheckboxFor for multiple values. I feel that this may be just a minor thing to fix, but I cannot figure it out for several days now. Here is my model: public

Model Binding with ASP.NET Core [duplicate]

喜你入骨 提交于 2020-05-27 13:18:51
问题 This question already has an answer here : Why is Model Binding not working in my POST action method? (1 answer) Closed 2 years ago . I'm trying to make a simple todo MVC application with asp.net core MVC. My plan is to make a webpage with simple checkboxes and give the user the ability to change the state with a form. The form is displayed correctly but the "model binding" in the controller does not work. What am I doing wrong? The UI (image) The view: @model List<Todo> <form asp-controller=

More complex(real-life) modelbinding?

感情迁移 提交于 2020-02-03 10:43:07
问题 Roll with me and imagine the following example: Public ViewResult GiveMeFruit(int personId, string personName, int personAge, int fruitId){ Person person = PersonService.GetPerson(personId); person.Name = personName; person.Age = age; person.Fruits.Add(FruitService.GetFruit(fruitId)); ViewData.Person = person; View(ViewData); } This should be done better like so Public ViewResult GiveMeFruit(Person person, IFruit fruit){ person.Fruits.Add(fruit); ViewData.Person = person; View(ViewData); } I

AngularJS custom directive within ng-repeat with dynamic attributes and two way binding

天涯浪子 提交于 2020-01-25 04:46:28
问题 I'm banging my head on the wall over this for days and finally decided to post this question since I can't find an answer that matches what I'm trying to do. Context: I'm building a dynamic form building platform that describes form elements in a JSON structure like this - { "name": "email", "type": "email", "text": "Your Email", "model": "user.profile.email" } And then in the View I have a recursive ng-repeat that includes the field template like this - <script type="text/ng-template" id=

Custom Model Binder for DropDownList not Selecting Correct Value

旧巷老猫 提交于 2020-01-24 19:33:09
问题 i've created my own custom model binder to handle a Section DropDownList defined in my view as: Html.DropDownListFor(m => m.Category.Section, new SelectList(Model.Sections, "SectionID", "SectionName"), "-- Please Select --") And here is my model binder: public class SectionModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if

Problems with Model binding and validation attributes with asp.net Web API

本秂侑毒 提交于 2020-01-23 17:55:26
问题 I am writing a Web API with ASP.NET Web API, and make use of the following View Model. I seem to be having a problem with the data binding when there are two validation attributes on a particular property (i.e. [Required] and [StringLength(10)]). When posting a JSON value from a client to a controller action of the form: // POST api/list public void Post([FromBody] TaskViewModel taskVM) I observe the following: If I remove one of the multiple attributes everything is bound OK; If I leave in

How to override the ASP.NET MVC 3 default model binder to resolve dependencies (using ninject) during model creation?

天大地大妈咪最大 提交于 2020-01-23 05:56:35
问题 I have an ASP.NET MVC 3 application that uses Ninject to resolve dependencies. All I've had to do so far is make the Global file inherit from NinjectHttpApplication and then override the CreateKernel method to map my dependency bindings. After that I am able to include interface dependencies in my MVC controller constructors and ninject is able to resolve them. All that is great. Now I would like to resolve dependencies in the model binder as well when it is creating an instance of my model,

ASP.Net MVC Dynamic input bound to same controller property

不羁的心 提交于 2020-01-17 01:35:07
问题 I have 2 controller fields say Type and Data. Depending on value selected for Type (Date or Text), I want to display Data field dynamically as either a text input or a custom timepicker input. Since only one will be rendered at any time, I need to bind with the same property name (Data). This is what I am trying: @if (Model.Type == "Date") { // custom timepicker control goes here <input asp-for="Data" class="form-control timepicker"/> } else { <input asp-for="Data" class="form-control text