model-binding

ASP.Net MVC Model Binding Complex Object using GET

☆樱花仙子☆ 提交于 2019-12-05 16:42:49
问题 I have a class in my web project: public class MyClass { public int? Param1 { get; set; } public int? Param2 { get; set; } } which is a parameter in my controller method: public ActionResult TheControllerMethod(MyClass myParam) { //etc. } If I call the method using POST, the model binding works automatically (I use angular on the js side, which likely doesn't matter): $http({ method: "post", url: controllerRoot + "TheControllerMethod", data: { myParam: myParam } }).success(function (data) {

Asp.Net MVC Dynamic Model Binding Prefix

北慕城南 提交于 2019-12-05 15:59:59
Is there any way of changing the binding prefix with a value which comes from the request parameters? I have many nested search popups, and all of them shares the same ViewModel. I can add a binding prefix to all the fields when requesting for the Search filters, but i don't know how can i make the [Bind(Prefix = "")] to work with values coming from the request parameters. // get the search filters with the bindingPrefix we need public ActionResult Search(string bindingPrefix) { ViewData.TemplateInfo.HtmlFieldPrefix = bindingPrefix; SearchViewModel model = new SearchViewModel { BindingPrefix =

asp.net web api - model binding list parameter

人盡茶涼 提交于 2019-12-05 14:30:36
In my controller I have: [AcceptVerbs("GET", "POST")] public List<BuzzMonitor.Web.Message> Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds) {...} and I want to do model binding. It's easy for primitive types, but what to do when I have a list of primitive types? I did it like this in Global.asax: GlobalConfiguration.Configuration.Routes.MapHttpRoute("SearchWithParameters", "api/{controller}/{action}/{text}/{dateFrom}/{dateTo}/?/?" But I dont know what to set for lists... I found on some site that I can add [ModelBinder] before list, but when I do

How specify two css classes: from property and conditional class

不羁的心 提交于 2019-12-05 13:43:01
问题 I know that in knockout has the ability to specify class from observable property, like this: <div data-bind="css: Color " > Knockout also provides the ability to specify conditional class rendering like this: <div data-bind="css: { 'my-class' : SomeBooleanProperty }" > But which markup should be specified if i need those features of knockout css binding together? I tried this, but with no luck: <div data-bind="css: { Color, 'my-class' : SomeBooleanProperty }" > I have got the error: Error:

Web API action parameter is intermittently null

梦想与她 提交于 2019-12-05 13:26:53
Related question: Web API ApiController PUT and POST methods receive null parameters intermittently Background While load testing an existing Web API project I noticed a lot of null reference exceptions as a result of a parameter being null when posting to an action. The cause seems to be a custom message handler registered to log requests while running in dev environments. Removing this handler resolves the issue. I understand that in Web API I can only read the request body once and that reading it would always cause my parameter to be null as model binding wouldn't be able to take place.

ASP.NET MVC 4, how to access/modify the view model object (and change view and action method) before it is used as action method parameter?

最后都变了- 提交于 2019-12-05 12:58:20
Is there any useful hook in ASP.NET MVC (MVC4) which can let you access the Action method parameter (View model) before the action method becomes invoked, and then also (e.g. depending on the value of something you checked in the action method parameter) let you prevent the action method from being invoked, i.e. instead either forward the view model object (action method parameter) to another action method or directly to some view (i.e. without any further processing in an action method) ? If you do not understand the question, please see the code example below which should illustrate the kind

ASP.NET MVC Model Binding Related Entities on Same Page

蓝咒 提交于 2019-12-05 10:13:25
This problem has been driving me crazy for several hours now... In my domain, I have 2 entities that are related to each other Sku and Item . Each sku can have many items. public class Sku { private readonly EntitySet<Item> items; public Sku() { items = new EntitySet<Item>(AttachItems, DetachItems); } public int SkuId { get; set; } public string LongDescription { get; set; } public EntitySet<Item> Items { get { return items; } set{ items.Assign(value);} } private void AttachItems(Item entity) { entity.Sku = this; } private static void DetachItems(Item entity) { entity.Sku = null; } } public

Model binding the sort field from KendoUI Grid

天大地大妈咪最大 提交于 2019-12-05 09:27:08
I'm using KendoUI Grid to show data. I have server paging working like a charm. The each page change in the kendo grid is a new ajax request to the server and the server returns the correct page of data. I am now trying to do server-side sorting, but I'm having trouble getting model binding to bind to the sort values. This is what the request from the Kendo Grid looks like: My action method looks like this: public JsonResult GetReports(int pageSize, int skip, List<KendoSort> sort) { // sort is not being populated with the right data. } KendoSort is a custom class: public class KendoSort {

ASP.NET MVC 3 Model Binding - ViewBag.Title clash with input of id=“Title”

一世执手 提交于 2019-12-05 09:06:23
There seems to be an issue with the ViewBag dynamic properties. Lets say I have: @{ ViewBag.Title = @Model.CourseName; } And then in a form on the page I have: @Html.TextBox("Title", null, new {style="width:400px;"}) Where Title is the name of a field in a database table. When the page first opens, text box with an id of "Title" takes the value of the ViewBag.Title dynamic property. I am a bit hazy on the exact details of Model Binding, but this does seem to be a bug, or if not, if it is something that occurs naturally as a result of the binding process, then it would be nice to be warned of

Validating Nested Models

流过昼夜 提交于 2019-12-05 08:34:50
I currently have a ViewModel setup as such: public class OurViewModel { public OurViewModel() { } [Required] public int LeadID { get; set; } [Required] public int Rate { get; set; } [Required] public bool DepositRequired { get; set; } [RequiredIfOtherPropertyIsTrue("DepositRequired")] public BankInfo { get; set; } } ...in this case, "RequiredIfOtherPropertyIsTrue" is a validator that does pretty much what it says: checks to see if another property is true (in this case, our boolean indicating whether or not a deposit is required), and BankInfo is another model that looks something like this: