model-binding

Model binding postback data to a controller action parameter of type List<T>

荒凉一梦 提交于 2019-12-11 03:09:58
问题 I have a strong type view of type List<List<MyViewModelClass>> The outer list will always have two lists of List<MyViewModelClass> . For each of the two outer lists I want to display a group of checkboxes. Each set can have an arbitrary number of choices. My view model class looks similar to this: public class MyViewModelClass { public Area Area { get; set; } public bool IsGeneric { get; set; } public string Code { get; set; } public bool IsChecked { get; set; } } So the final view will look

Get custom attribute for parameter when model binding

折月煮酒 提交于 2019-12-11 02:46:38
问题 I've seen a lot of similar posts on this, but haven't found the answer specific to controller parameters . I've written a custom attribute called AliasAttribute that allows me to define aliases for parameters during model binding. So for example if I have: public JsonResult EmailCheck(string email) on the server and I want the email parameter to be bound to fields named PrimaryEmail or SomeCrazyEmail I can "map" this using the aliasattribute like this: public JsonResult EmailCheck([Alias

Bind Exclude not working Model Binding for child objects in ASP.Net MVC

坚强是说给别人听的谎言 提交于 2019-12-11 02:44:30
问题 I got into a issue in Model Binding in Asp.Net MVC. I have view model like below, public class ArticleViewModel : BaseViewModel { public Article art { get; set; } public List<ArticleAttachment> attachments { get; set; } } I am trying to exclude model binding a property on the "Article" child object as seen below in my action method, [HttpPost] [ValidateInput(false)] public ActionResult New([Bind(Exclude = "art.Abstract")]ArticleViewModel articleVM) { But the model binder populates the

ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName) returns null

最后都变了- 提交于 2019-12-11 02:43:31
问题 I have bindingContext.ValueProvider.GetValue(bindingContext.ModelName) for ModelBinding and it returns null, but if I use bindingContext.ValueProvider.GetValue("id") is returns the correct record. Any Idea what's missing? Am I supposed to register the model class somehow? public class EntityModelBinder<TEntity>: IModelBinder where TEntity : Entity { private readonly IUnitOfWork unitOfWork; public EntityModelBinder(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } public object

Custom Model Binder to bind nested property values

和自甴很熟 提交于 2019-12-11 02:03:27
问题 The reason I need this: In one of my controllers I want to bind all Decimal values in a different way than the rest of my application. I do not want to register a Model Binder in Global.asax (via ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); ) I have tried deriving from the DefaultModelBinder class and override its BindProperty method, but that only works for the model instance's immediate (not nested) Decimal properties. I have the following example to demonstrate my

Model Binding - Type in External Assembly

此生再无相见时 提交于 2019-12-11 01:19:06
问题 I have a type in an assembly which isn't referenced by the core library but is referenced from the web application. e.g. namespace MyApp.Models { public class LatestPosts { public int NumPosts { get; set; } } } Now i have the following code in the core library: [HttpPost, ValidateAntiForgeryToken] public ActionResult NewWidget(FormCollection collection) { var activator = Activator.CreateInstance("AssemblyName", "MyApp.Models.LatestPosts"); var latestPosts = activator.Unwrap(); // Try and

ASP.NET GridView Model-binding

回眸只為那壹抹淺笑 提交于 2019-12-11 01:04:46
问题 I am new to ASP.NET and I want to write a simple WebApplication using WebForms that connects to a Database and displays some data in a Grid (with paging). I use Visual Studio 2015 For displaying the data I use a GridView in combination with an ObjectDataSource. I use EntityFramework and a method GetCustomer() that returns all Customers from a database.I use that method as SelectMethod My question is: Both controls, GridView an ObjectDataSource, have parameters for Paging and SelectMethod

Getting my Custom Model bound to my POST controller

血红的双手。 提交于 2019-12-10 18:31:43
问题 I am using the recently released MVC 4 Beta (4.0.20126.16343) and am working on getting around a known problem with deserialization/model binding not working with arrays (see Stack Overflow here) I am having difficulty getting my explicit custom binding to hook up. I have registered a custome IModelBinder (or attempted to) but when my post action is called my custom binder isn't hit and I just get the default serialization (with the null arrays - even though wireshark shows me the incoming

TimeSpan and “24:00” parse error in Asp.net MVC

可紊 提交于 2019-12-10 17:34:14
问题 I have a modal dialog in my web application where users are able to enter a time range between 00:00 and 24:00. A range slider is used to select this range. Everything works as expected except that whenever user sets the right range handle to have a value of 24:00 default model binder can't parse this TimeSpan . public class Timing { public TimeSpan Starts { get; set; } public TimeSpan Ends { get; set; } } My object that gets sent back to server has an IList<Timing> property. So. The problem

MVC 3 passing entity as an Interface

不羁岁月 提交于 2019-12-10 14:44:42
问题 I'm currently working on an MVC 3 project using Ninject as my DI, the business objects are stored in a separate assembly. I'm running into an issue with the controller parameters, when posting back for CRUD operations I'm getting the error "Cannot create an instance of an interface". I am aware that you can't create an instance of an interface, but it seems like the only way I can get around this is to use a custom model binder and pass the FormCollection through. This seems really messy and