model-binding

.NET core custom and default binding combined

主宰稳场 提交于 2019-12-07 04:00:48
问题 I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync

Model Bind to a List<> when Posting JavaScript Data Object

为君一笑 提交于 2019-12-06 22:12:43
I'm trying to post a JavaScript data object with the following: $.post(frm.attr("action"), data, function(res) { // do some stuff }, "json"); where 'data' takes the structure of data - panelId - siteId - ConfiguredFactsheetId // this is an array of CheckBox ids that correspond to ConfiguredFactsheets - 123 - 234 - 345 With this, both site & panel are correctly instantiated & bound with their data but the List object is null. public JsonResult Edit(Site site, Panel panel, List<ConfiguredFactsheet> configuredFactsheets) { // do stuff return Json(success); } Now, I realise that my 'data' object's

What's the WebApi [FromUri] equivalent in ASP.NET MVC?

巧了我就是萌 提交于 2019-12-06 16:51:12
问题 In WebApi I can decorate a parameter on a controller action with [FromUri] to have the components of the URI 'deserialized', if you will, into a POCO model; aka model-binding. Despite using MVC since 2.0, I've never used it for websites (dunno why). What's the equivalent of it in ASP.NET MVC 5? The attribute doesn't seem to be recognised in the IDE unless I need to reference a library. I'd like ~/thing/2014/9 to bind to the model below: public class WhateverModel { public int Year { get; set;

Polymorphic custom model binder not populating model w/ values

不羁的心 提交于 2019-12-06 14:32:40
问题 I have a custom model binder that I'm using to return the appropriate model sub-type based on a hidden value containing the original type. For example, in my view (EditorTemplate) I have: @model MyWebApp.Models.TruckModel @Html.Hidden("ModelType", Model.GetType()) @Html.EditorFor(m => m.CabSize) Then, in my custom model binder, I have: protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) { var typeValue = bindingContext

ModelBinding parameter list in MVC

拈花ヽ惹草 提交于 2019-12-06 14:10:44
I'm trying to use jQuery Datatables 1.10 with server side processing and ASP.NET MVC 5. Unfortunately, Datatables 1.10 writes to the server by serializing a complex hierarchy of objects into a param string ( Content-Type: application/x-www-form-urlencoded ), which breaks the MVC ModelBinder . Is there any way, in MVC, to bind a complex hierarchy of objects passes as a serialized parameter list? Perhaps a custom ModelBinder that someone has written? Note: Setting traditional = true in the ajax call doesn't work, because that doesn't support hierarchies of objects. I end up with something like:

Custom model binding on derived property not working

馋奶兔 提交于 2019-12-06 11:32:50
I have a custom ModelBinder (MVC3) that isn't getting fired for some reason. Here are the relevant pieces of code: View @model WebApp.Models.InfoModel @using Html.BeginForm() { @Html.EditorFor(m => m.Truck) } EditorTemplate @model WebApp.Models.TruckModel @Html.EditorFor(m => m.CabSize) ModelBinder public class TruckModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { throw new NotImplementedException(); } } Global.asax protected void Application_Start() { ... ModelBinders.Binders.Add(typeof(TruckModel), new

Custom model binder with inheritance using Web API and RavenDB

本小妞迷上赌 提交于 2019-12-06 11:27:42
问题 I'm developing a simple web app where I need to bind all types implementing and interface of a specific type. My interface has one single property like this public interface IContent { string Id { get;set; } } a common class using this interface would look like this public class Article : IContent { public string Id { get;set; } public string Heading { get;set; } } to be clean here the article class is just one of many different classes implementing IContent so therefor I need a generic way

Why does WebApi fail to bind a System.Version parameter?

余生长醉 提交于 2019-12-06 10:46:58
UPDATE : solved! Nothing to see here, please move on :-) I have an ApiController method that takes a System.Version parameter. The parameter is passed in the request body, as JSON. This is what gets sent: { "Major": 0, "Minor": 7, "Build": 0, "Revision": 0, "MajorRevision": 0, "MinorRevision": 0 } The routing works - my method is being called - but the parameter has an empty Version object (all values zero). Why? Here's the declaration of the controller method: // POST api/service/details [HttpPost] [ActionName("Details")] public ServiceDto Get(Version version) { } To prevent anyone else from

ASP.net MVC - Use Model Binder without query string values or fancy routes

送分小仙女□ 提交于 2019-12-06 10:31:57
I have an Asp.net MVC app that currently works well using the default model binder and urls with complex parameters like this: example.com/Controller/Action?a=hello&b=world&c=1&d=2&e=3 (notice the question mark) The different urls automatically map to Action Method parameters using the built in model binder. I would like to continue using the standard model binder but I need to get rid of the query string. We want to put these urls behind a CDN that does not support resources that vary by query strings (Amazon Cloud front) so we need to remove the question mark from our urls and do something

Question about JSON and Serialization

点点圈 提交于 2019-12-06 10:30:29
I have a strongly typed view with my viewmodel which contains an object Person and an List of Skills. Person is quite straightforward. I use Html Helpers like this @Html.TextBoxFor(m => m.Person.FirstName) . I submit my form and get what I want. The problem is my List of skills. With an AJAX call i get a JSON result which is an array of Skills. How should I send the combination of both the skills and the person to my HTTPPOST method? I see two possibilities. The one I favour, but have no idea on how to implement in the correct way: Somehow manage to get this JSON result into my viewmodel (