model-binding

Creating a ModelBinder for MongoDB ObjectId on Asp.Net Core

落花浮王杯 提交于 2019-12-10 14:31:40
问题 I'm trying to create a very simple model binder for ObjectId types in my models but can't seem to make it work so far. Here's the model binder: public class ObjectIdModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { var result = bindingContext.ValueProvider.GetValue(bindingContext.FieldName); return Task.FromResult(new ObjectId(result.FirstValue)); } } This is the ModelBinderProvider I've coded: public class ObjectIdModelBinderProvider :

ASP.NET Web API Model Binder for Generic Type

别说谁变了你拦得住时间么 提交于 2019-12-10 14:25:16
问题 In line with counterpart MVC question, is there a way to create a model binder for generic types in ASP.NET Web API? If yes, how would one handle type checking and instantiation in the binder? Assume model binder is for URL parameters, consider you have [ModelBinder(typeof(MyTypeModelBinder))] public class MyType<T> { //... } And public class MyTypeModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { // check if type if

ModelBinding: POST data (possibly from Ruby) in MVC4/C#

£可爱£侵袭症+ 提交于 2019-12-10 13:52:45
问题 We're integrating with chargify http://www.chargify.com and I need to handle webhooks from Chargify in our MVC4/C# server. Chargify sends POST data in the (ruby) way - sub objects are delimited in square brackets, like so: POST /1ffaj2f1 HTTP/1.1 X-Chargify-Webhook-Signature: 526ccfd9677668674eaa6ba5d447e93a X-Chargify-Webhook-Id: 11238622 User-Agent: Ruby Host: requestb.in Content-Type: application/x-www-form-urlencoded Content-Length: 5159 Connection: close Accept-Encoding: gzip, deflate

Change default NumberStyles on integers?

£可爱£侵袭症+ 提交于 2019-12-10 13:34:50
问题 I have a model that has an integer property. When the model is submitted with 23443 , the model binder works great and the value is available in the action. But if the model is submitted with a thousands separator like 23,443 , the value isn't parsed and the property is zero. But I found that a property with the type of decimal could have the thousands separator and it would parse and be populated correctly. I found out that by default Int32.Parse() doesn't parse thousands separator but

Modelbinding with SelectList

筅森魡賤 提交于 2019-12-10 11:37:28
问题 I create a DropDown with the Html.DropDownList(string NameSelectListInViewData) method. This generates a valid Select input with the correct values. And all is well. Upon submit however, the value in the source SelectList is not bound. Case: ViewData.SearchBag.FamilyCodes: public SelectList FamilyCodes { get; set; } Html that generates the dropdown: <%=Html.DropDownList("SearchBag.FamilyCodes")%> Generated html: <select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes"> <option value="

how do you go about dealing with ajax and Model Binding when complex types are involved?

允我心安 提交于 2019-12-10 11:34:45
问题 I would like to keep model binding when doing ajax calls from my view. I'm using jquery ajax to make the calls, and just as an example- this is my controller method I want to call from ajax: public ActionResult Create(Person personToCreate) { //Create person here } As you can see, this method is relying on Model Binding. This makes the method a lot cleaner... however, this means that when the ajax makes a call, it must supply all variables that are non-nullable in the DB. So, if I have a

DataTable Model Bind in Mvc

妖精的绣舞 提交于 2019-12-10 10:56:11
问题 I'm using asp.net mvc with aspx view engine to read from an excel file, extract contact information from that, display it to the user and when he\she confirms it would save them in database. Now in my page I got my DataTable as Model and with a foreach loop I'm showing that to the user. then I have a submit button that when the user click on it I want my datatable back to the Action in a Controller. but my Datatable is null. How can I have my datatable back to my controller ? aspx first line

MVC3 RC2 JSON Post Binding not working correctly

点点圈 提交于 2019-12-10 09:34:57
问题 I've seen other posts on this subject and have fiddled with variations but still cannot not get the JSON model binding to work correctly. I have the following in my global.asax.cs Application_Start method: ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); The post back data looks like this: {"UserName":"Mike","Password":"password","Persist":true} My PoCo: public class UserLoginViewModel { public string UserName { get; set; } public string Password { get; set; } public bool

ASP.NET MVC Model Binding Related Entities on Same Page

。_饼干妹妹 提交于 2019-12-10 04:59:34
问题 This problem has been driving me crazy for several hours now... In my domain, I have 2 entities that are related to each other Sku and Item . Each sku can have many items. public class Sku { private readonly EntitySet<Item> items; public Sku() { items = new EntitySet<Item>(AttachItems, DetachItems); } public int SkuId { get; set; } public string LongDescription { get; set; } public EntitySet<Item> Items { get { return items; } set{ items.Assign(value);} } private void AttachItems(Item entity)

MVC3 How To Bind Multiple Checkboxes to 1 Property in ViewModel

本秂侑毒 提交于 2019-12-10 04:10:12
问题 I need to display a list of checkboxes, which more than one can be checked. When the user hits submit, the value of these checkboxes need to go into a property in the ViewModel...this is what I got so far... public class RegisterModel { public List<string> Roles { get; set; } public List<RoleModel> SelectedRoles { get; set; } } public class RoleModel { public string RoleName { get; set; } } In the view I am trying to do this... @foreach (var role in Model.Roles) { @Html.CheckBoxFor(m => m