model-binding

ASP.Net MVC 3 - JSON Model binding to array

♀尐吖头ヾ 提交于 2019-11-30 03:19:15
I am on ASP.Net MVC 3, and going by the feature list supported in at, i should be able to get default json model binding working out of the box. However i havent been successful in binding an array/collection from json to the action method parameter. Although I did get simple json object binding working right. Would greatly appreciate if an expert here could tell me what i am doing wrong. Here is the code: Server side code first: //Action Method public JsonResult SaveDiscount(IList<Discount> discounts) { foreach(var discount in discounts) { .... } } //View model public class Discount { string

Bind query parameters to a model in ASP.NET Core

霸气de小男生 提交于 2019-11-30 00:43:06
问题 I am trying to use model binding from query parameters to an object for searching. My search object is [DataContract] public class Criteria { [DataMember(Name = "first_name")] public string FirstName { get; set; } } My controller has the following action [Route("users")] public class UserController : Controller { [HttpGet("search")] public IActionResult Search([FromQuery] Criteria criteria) { ... } } When I call the endpoint as follows .../users/search?first_name=dave the criteria property on

ASP.NET MVC3 JSON Model-binding with nested class

眉间皱痕 提交于 2019-11-30 00:31:37
In MVC3, is it possible to automatically bind javascript objects to models if the model has nested objects? My model looks like this: public class Tweet { public Tweet() { Coordinates = new Geo(); } public string Id { get; set; } public string User { get; set; } public DateTime Created { get; set; } public string Text { get; set; } public Geo Coordinates { get; set; } } public class Geo { public Geo(){} public Geo(double? lat, double? lng) { this.Latitude = lat; this.Longitude = lng; } public double? Latitude { get; set; } public double? Longitude { get; set; } public bool HasValue { get {

Model Bind List of Enum Flags

感情迁移 提交于 2019-11-29 19:33:42
I have a grid of Enum Flags in which each record is a row of checkboxes to determine that record's flag values. This is a list of notifications that the system offers and the user can pick (for each one) how they want them delivered: [Flag] public enum NotificationDeliveryType { InSystem = 1, Email = 2, Text = 4 } I found this article but he's getting back a single flag value and he's binding it in the controller like this (with a days of the week concept): [HttpPost] public ActionResult MyPostedPage(MyModel model) { //I moved the logic for setting this into a helper //because this could be re

Binding an editable list of children

喜夏-厌秋 提交于 2019-11-29 19:03:57
问题 TL;DR : In my ASP.NET MVC3 App, how should I implement a View that allows me to edit details of a 'parent' entity at the same time as the details of a list of 'children' entities ? Update : I'm accepting @torm's answer because he provided a link that gives some explanation as to why my current solution may be as good as it gets. However, we'd love to hear if anyone else have any alternative! I've been searching and reading (see the 'References' section at the bottom for some of the findings

Dictionary<short, Dictionary<EnFunction, bool>> model binding not work

本小妞迷上赌 提交于 2019-11-29 16:41:01
I could not bind the model to Controller. Please give me any advice. Model,controller and view classes are below. When model is submmited, dictionary property equals to null. public class GroupRights //model { public List<DtoGrup> groups { get; set; } public Dictionary<short, Dictionary<EnFunction, bool>> groupRights { get; set; } // group function HasPermission } public enum EnFunction { LPDU_login, LPDU_changePassword, LPDU_transitList, LPDU_PosEventList, .... } Controller public ActionResult GroupRights() { TocCommonService.CommonServiceClient client = new TocCommonService

Model binding in the controller when form is posted - navigation properties are not loaded automatically

陌路散爱 提交于 2019-11-29 15:53:47
I'm using the Entity Framework version 4.2. There are two classes in my small test app: public class TestParent { public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual ICollection<TestChild> TestChildren { get; set; } } public class TestChild { public int TestChildID { get; set; } public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual TestParent TestParent { get; set; } } Populating objects with data from the database works well. So I can use testParent

How to dynamically create/remove elements and allow model binding to kick in?

你离开我真会死。 提交于 2019-11-29 15:35:32
问题 I've done this in the past, and i may have to do it again, but before i do, i want to throw it out there to see how people handle it. Razor view: <ul> @Html.EditorFor(model => model.Questions) </ul> Which could produce: <ul> <li><input type="text" id="Questions_0__Title" name="Questions[0].Title" value="hi"/></li> <li><input type="text" id="Questions_1__Title" name="Questions[1].Title" value="hi2"/></li> <ul> Pretty dead simple. Now, i need to allow the user to add, edit or remove any of

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

How to bind view model property with different name

*爱你&永不变心* 提交于 2019-11-29 11:31:46
问题 Is there a way to make a reflection for a view model property as an element with different name and id values on the html side. That is the main question of what I want to achieve. So the basic introduction for the question is like: 1- I have a view model (as an example) which created for a filter operation in view side. public class FilterViewModel { public string FilterParameter { get; set; } } 2- I have a controller action which is created for GETting form values(here it is filter) public