modelbinders

ASP.Net Web Api not binding model on POST

拥有回忆 提交于 2019-12-08 15:01:16
问题 I'm trying to POST JSON data to a Web Api method but the JSON data is not binding to the model. Here's my model: [DataContract] public class RegisterDataModel { [DataMember(IsRequired = true)] public String SiteKey { get; set; } [DataMember(IsRequired = true)] public String UserId { get; set; } [DataMember(IsRequired = true)] public String UserName { get; set; } } Here's my Web Api action: public class RegisterController : ApiController { public Guid Post([ModelBinder] RegisterDataModel

ASP.NET MVC Session

我的未来我决定 提交于 2019-12-08 09:54:22
问题 I've been reading PRO ASP.NET MVC2 by Steven Sanderson and I still can't figure out something about session. In the book he tells how to develop a Cart based on session using a Custom model binder for session persisting. Everything works fine but I can't figure out how it really works under the hood. Since it's a fair amount of code I'll write a simplified version Counter public class Counter { public int counter = 0; public void Increment(){ counter++; } } CounterController public

dropdown value null when using, viewmodel & modelbinder in asp.net mvc

本秂侑毒 提交于 2019-12-08 06:35:52
问题 I am using asp.net's modelbinder functionality to bind form values to my entity when posting from a view. The html renders correctly in the initial view with correct option and value items. When completing the form and posting, all values are populated correctly into the entity except the value from the dropdown list. not sure what I am doing wrong. code attached below: Customer Entity: public class Customer : EntityBase { public virtual string Name { get; set; } public virtual string Email {

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

孤街浪徒 提交于 2019-12-08 00:57:21
问题 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

Model Binding within a Model Binder

柔情痞子 提交于 2019-12-06 15:15:07
问题 Firstly, bear with me here. I have a custom model binder which is successfully mapping form data to a custom object. Within this model binder it also maps form items to different custom object. What I feel I should be able to do is create a separate model binder to take care of this second mapping. This is a simplified version. Custom objects: public class Category { public int CategoryId { get; set; } public string Name { get; set; } public string Status { get; set; } public string

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

ASP.NET MVC: avoid tight coupling when binding form POST to parameter

大城市里の小女人 提交于 2019-12-06 09:39:36
问题 Let's say I have an interface like: interface IThing { int Id { get; set; } string Title { get; set; } } And in ASP.NET MVC I have a form that posts to a controller action like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult NewThing([Bind(Exclude = "Id")] SimpleThing thing) { // code to validate and persist the thing can go here } Where SimpleThing is a concrete class that just barely implements IThing . However, I would like all my methods to deal with the interface. I have a data

Is there a way to have the DefaultModelBinder ignore empty items when binding to a List<Enum>

烈酒焚心 提交于 2019-12-06 07:39:14
问题 I have a scenario where I'd like to change the behavior of the DefaultModelBinder in how it binds to a List of enums. I have an enum... public enum MyEnum { FirstVal, SecondVal, ThirdVal } and a class for a model... public class MyModel { public List<MyEnum> MyEnums { get; set; } } and the POST body is... MyEnums=&MyEnums=ThirdVal Currently, after model binding, the MyEnums property will contain... [0] = FirstVal [1] = ThirdVal Is there was a way to tell the model binder to ignore the empty

Retrieving data from view, should I use model binder?

点点圈 提交于 2019-12-06 07:24:20
问题 I am a bit lost here as I have not really looked at model binders so if possible, can one advise me if I am actually thinking about my problem correctly... :) and if my code is way of, please advise... 1 -I have a DTO class which contains 'custom fields' each with a name and other properties i.e.: Public Class CustomFields { public string Name {get;set;} public string Description {get;set;} public string FieldType {get;set;} public string Value {get;set;} } 2- Within my repo/business layer I

Is it okay to hit the database from a custom model binder?

孤人 提交于 2019-12-06 06:34:59
问题 Say I have an object that gets some data from HttpPost and some from the database. I think I want to allow the ModelBinder to go to the database/repository for the that data missing from the post. In practice, is this a good or bad idea? 回答1: I've decided to edit my original answer given my thinking on these types of things has evolved since early 2010. In my original answer, I basically expressed that, while my instincts told me you shouldn't do this, I was uncomfortable saying we shouldn't