model-binding

DefaultModelBinder and collections of complex types [duplicate]

落花浮王杯 提交于 2019-12-25 02:15:50
问题 This question already has answers here : Calling UpdateModel with a collection of complex data types reset all non-bound values? (3 answers) Closed 3 years ago . I have the following classes: public class ComplexType { public long? Code { get; set; } public string Name { get; set; } } public class TestViewModel { public List<SubModel> List { get; set; } public ComplexType ComplexTypeTwo { get; set; } public string StringTwo { get; set; } } public class SubModel { public ComplexType

How would I create a model binder to bind an int array?

倖福魔咒の 提交于 2019-12-25 01:59:47
问题 I'm creating a GET endpoint in my ASP.NET MVC web API project which is intended to take a an integer array in the URL like this: api.mything.com/stuff/2,3,4,5 This URL is served by an action that takes an int[] parameter: public string Get(int[] ids) By default, the model binding doesn't work - ids is just null. So I created a model binder which creates an int[] from a comma-delimited list. Easy. But I can't get the model binder to trigger. I created a model binder provider like this: public

MVC2 Action to handle multiple models

℡╲_俬逩灬. 提交于 2019-12-24 19:02:13
问题 I've been looking around for an answer to this, which I can't believe hasn't been asked before, but with no luck I'm attempting here. I have a signup form which differs slightly based upon what type of participant the requester is. While writing tests for the solution, I realized that all actions did the same things, so I'm attempting to combine the actions into one using a strategy pattern. public abstract class BaseForm { common properties and methods } public class Form1 : BaseForm {

Validating parameters in ASP.NET Web API model binder

↘锁芯ラ 提交于 2019-12-24 16:05:05
问题 I have a custom IModelBinder for my model: public class MyBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { // bla-bla-bla bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Request value is invalid."); return false; } } I expect that when invalid value is passed in a request HTTP 400 Bad Request is returned automatically. However, this does not happen. What should I do to make Web API return HTTP 400 if there

Web forms model binding child collection

℡╲_俬逩灬. 提交于 2019-12-24 15:36:52
问题 I can't figure out how to use model binding to update a child collection like I can in MVC. All the collection-type controls seem to assume some server-side storage mechanism using an edit/update lifecycle, which doesn't fit if this is supposed to be an insert form where I want to store the child collection in the form itself. public class BarViewModel { public string Name {get;set;} } public class FooViewModel { public string Description {get;set;} public List<BarViewModel> Bars {get;set;} }

MVC. Bind TextBoxFor to a Field of type object

心不动则不痛 提交于 2019-12-24 14:26:29
问题 Suppose the following code: public class Person { public object Age { get; set;} } Inside view: @Html.TextBoxFor(x => x.Age, new { @type = "number" }) Now when posting the Form, the typeOf person.Age property is string[1] . Why? Shouldn't there be some input type based, Type pick-up logic, when binding things? 回答1: You can't use the *For helpers with a property of type object . These helpers need to do type inference in order to create the right type of input with the right name and

Raw POST data deserialization using C#/.net (bind to complex model) like in modelbinder of MVC/webApi

本小妞迷上赌 提交于 2019-12-24 12:05:18
问题 I would like to have any ready generic solution to convert string with POST data like : "id=123&eventName=eventName&site[domain][id]=123" to my complex object public class ComplObject { public int id {get;set;} public string eventName {get;set;} public siteClass site{get;set;} } public class siteClass { public domainClass domain {get;set;} } public class domainClass { public int id {get;set;} } Access to asp.net MVC reference is allowed. Looks,like i need standalone formdata binder, but i

ASP.Net MVC3 Parent Child Model Binding

僤鯓⒐⒋嵵緔 提交于 2019-12-24 09:56:12
问题 I have a partial template that uses a User object as a model. The user has a collection of Accounts. On this partial template I have a loop as follows. The _Account partial template is bound to the Account class @foreach (var item in Model.Accounts) { <tr> <td colspan="6"> <div> @Html.Partial("_Account", item) </div> </td> </tr> } In my controller method I initially tried [AcceptVerbs(HttpVerbs.Post)] public ActionResult UserDetails(User user, string actionType) But the User.Accounts

asp.net mvc model binding fails for <List> items in editor template

↘锁芯ラ 提交于 2019-12-24 08:57:53
问题 I've searched many posts on SO and still not sure what I did wrong here. I have a model for "Order" which includes a <list> of "OrderItem" public class Order { public int OrderId { get; set; } public int CustId { get; set; } public DateTime OrderDate { get; set; } public int OrderType { get; set; } ... ... public List<OrderItem> OrderItems = new List<OrderItem>(); } public class OrderItem { public string ProductCode { get; set; } public decimal RetailPrice { get; set; } public string

Model binding with complex type

陌路散爱 提交于 2019-12-24 04:25:11
问题 I have made a test controller and view to test complex binding, but I can't seem to make it work. Here is my ViewModel: public class TestViewModel { public SubTest MainTest { get; set; } public List<SubTest> SubTestList { get; set; } } public class SubTest { public string Name { get; set; } public int Id { get; set; } } Here is my View: @model TestViewModel @{ using (Html.BeginForm()) { <h2>Main</h2> <p> @Html.DisplayTextFor(m => m.MainTest.Id) => @Html.DisplayTextFor(m => m.MainTest.Name) <