modelbinders

DataContract model binding to JSON in ASP.NET MVC Action Method Arguments

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:59:01
MVC3 comes out of the box with JsonValueProviderFactory() which is very handy for binding incoming JSON to a model. Unfortunately, I can't figure out how to setup model contracts with names that differ from the incoming JSON. For example: [DataContract(Name = "session")] public class FacebookSession { [DataMember(Name = "access_token")] public string AccessToken { get; set; } [DataMember(Name = "expires")] public int? Expires { get; set; } [DataMember(Name = "secret")] public string Secret { get; set; } [DataMember(Name = "session_key")] public string Sessionkey { get; set; } [DataMember(Name

What would cause this custom XML ModelBinder to not deserialize my XML POST?

独自空忆成欢 提交于 2019-12-04 04:47:56
问题 The Model public class SimpleUser { public string FirstName { get; set; } public string LastName { get; set; } public string UserName { get; set; } public int Role { get; set; } public bool isActive { get; set; } public string Groups { get; set; } } The BinderProvider public class SimpleUserProvider : IModelBinderProvider { public IModelBinder GetBinder(Type modelType) { var contentType = HttpContext.Current.Request.ContentType; if (string.Compare(contentType, @"text/xml", StringComparison

Bind a routevalue to a property of an object that is part of viewmodel

£可爱£侵袭症+ 提交于 2019-12-03 22:44:41
问题 I have the following route: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); and I use the ViewModel: namespace MvcApplication1.Models { public class ProductViewModel { public ProductViewModel() { ProductDetail = new ProductInfo(); } public ProductInfo ProductDetail { get; set; } } public class ProductInfo { public string Name { get; set; } public int ProductID { get;

Asp.net mvc 3 - Custom model binding

六月ゝ 毕业季﹏ 提交于 2019-12-03 13:53:51
问题 I have a model like this public string Name { get; set; } public IEnumerable<int> ProjectMembersId { get; set; } The property Name should be bound using the standart binding code. But the property ProjectMembersId should be bound using my custom code. So I derived a class from the DefaultModelBinder and overrided the SetProperty method. protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor

Is there any way to disable the JSON ModelBinder in ASP.NET MVC 3 RC2?

穿精又带淫゛_ 提交于 2019-12-03 12:13:09
问题 In ASP.NET MVC 3 RC2, the default ModelBinder will automatically parse the request body if the Content-Type is set to application/json . Problem is, this leaves the Request.InputStream at the end of the stream. This means that if you try to read the input stream using your own code, you first have reset it back to the beginning: // client sends HTTP request with Content-Type: application/json and a JSON // string in the body // requestBody is null because the stream is already at the end var

What is the difference between BindProperty and SetProperty on IModelBinder

偶尔善良 提交于 2019-12-03 11:42:38
I'm creating a custom model binder in an Mvc application and I want to parse a string to an enumeration value and assign it to the model property. I have got it working overriding the BindProperty method, but I also noticed that there is a SetProperty method. protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { switch (propertyDescriptor.Name) { case "EnumProperty": BindEnumProperty(controllerContext, bindingContext); break; } base.BindProperty(controllerContext,

ASP.NET MVC 2 - ViewModel Prefix

社会主义新天地 提交于 2019-12-03 11:04:22
I want to use RenderPartial twice in my view with different models associated. The problem is that some properties are present in both models (nickname, password). They have no prefix, so even the id's or names are equal in the output. Now, if I have model errors for nickname or password, both fields get highlighted. Main View: <div> <% Html.RenderPartial("Register", Model.RegisterModel); %> </div> <div> <% Html.RenderPartial("Login", Model.LoginModel); %> </div> Login PartialView: <% using (Html.BeginForm("Login", "Member")) { %> <fieldset> <legend>Login</legend> <p> <%= Html.LabelFor(x => x

How to Unit Test a custom ModelBinder using Moq?

允我心安 提交于 2019-12-03 07:24:42
问题 I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted here. The problem I'm having is getting the Mocking all setup using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correctly. I think. Could someone help me figure out what I'm not doing correclty? Here's a sample of the Unit Test I'm trying to write that doesn't work: [TestMethod

MVVM and ModelBinders in the ASP.NET MVC Framework

China☆狼群 提交于 2019-12-03 04:38:43
问题 I've got a series of views, each are typed to have their own ViewModel class which contains everything they need to display themselves, for example: public class CreateResourceViewModel { public Project Parent { get; set; } public SelectList Categories { get; set; } public Resource Resource { get; set; } } The post action method for this I'd like to use would look like this: [AcceptVerbs (HttpVerbs.Post)] public ActionResult Create (Resource resource) { // Update code... } Notice that the

Asp.net mvc 3 - Custom model binding

社会主义新天地 提交于 2019-12-03 03:52:20
I have a model like this public string Name { get; set; } public IEnumerable<int> ProjectMembersId { get; set; } The property Name should be bound using the standart binding code. But the property ProjectMembersId should be bound using my custom code. So I derived a class from the DefaultModelBinder and overrided the SetProperty method. protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value) { if (propertyDescriptor.Name == "ProjectMembersId") { var list = new List