modelbinders

Modelbinding lists

只谈情不闲聊 提交于 2019-12-14 04:22:14
问题 I got a controller action like public class Question { public int Id { get;set; } public string Question { get;set; } public string Answer { get;set; } } public ActionResult Questions() { return View(GetQuestions()); } public ActionResult SaveAnswers(List<Question> answers) { ... } the view> looks like: <% for (int i = 0; i < Model.Count; i++) { %> <div> <%= Html.Hidden(i.ToString() + ".Id") %> <%= Model[i].Question %> <%= Html.TextBox(i.ToString() + ".Answer") %> </div> <% } %> Obviously

Why is my project building with an unknown Attribute?

旧街凉风 提交于 2019-12-13 02:58:46
问题 So, my code seems to be compiling without errors when it should not compile. Here, this project has no NuGet packages for AspNetCore.Mvc and hence this should not compile but still it does. Notice, the ModleBinder is in white color which means it does not recognize this type - still it is compiling. In another console app of the same VS this behaves fine. I am using VS 2017 15.4 Some of the things I have already done restarted VS many times Deleted the .vs folder Cleaned and Rebuilt the

ASP.NET MVC Model Binding into a List

こ雲淡風輕ζ 提交于 2019-12-12 07:06:51
问题 In my ASP.NET MVC site, part of a feature allows the user to enter the hours when a certain venue is open. I've decided to store these hours in a VenueHours table in my database, with a FK-to-PK relationship to a Venues table, as well as DayOfWeek, OpeningTime, and ClosingTime parameters. In my View, I want to allow the user to only input the times they know about; in other words, some days may not be filled in for a Venue. I'm thinking of creating checkboxes that the user can check to enable

ASP .NET Web API ModelBinder single parameter

蓝咒 提交于 2019-12-11 15:42:54
问题 Currently I've got this ModelBinder that works just fine: public class FooModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var body = JObject.Parse(actionContext.Request.Content.ReadAsStringAsync().Result); IEnumerable<Foo1> media = new List<Foo1>(); var transaction = body.ToObject<Foo2>(); media = body["Media"].ToObject<List<Foo3>>(); transaction.Media = media; bindingContext.Model = transaction; return true; } } As you

Custom ModelBinder Lifecycle and Dependency Injection [duplicate]

冷暖自知 提交于 2019-12-11 12:57:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Inject a dependency into a custom model binder and using InRequestScope using Ninject I'm trying to bind an NHibernate session to a custom model binder: Since a custom model binder appears to be a singleton, I think I need to be concerned with thread safety. This is my current IoC code: kernel.Bind<ISession>().ToProvider<SessionProvider>().InRequestScope() .OnActivation(x => ServiceModelBinder.Service = kernel

ASP.NET MVC Issue with Using Reflection Created Objects with the Default Model Binder

人走茶凉 提交于 2019-12-11 05:34:36
问题 I am having a weird issue in ASP.NET MVC with objects not being updated with UpdateModel when passed a formCollection . UpdateModel does not appear to be working properly when the object being updated is created through reflection. Scenario: I have an application which has approximately 50 lookup tables--each of which includes exactly the same schema including typical fields like id, title, description, isactive, and createdon. Rather than build 50 views, I wanted to have a single view which

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

In ASP.NET MVC 2, can I deserialize a querystring into an array using the default ModelBinder?

為{幸葍}努か 提交于 2019-12-10 17:12:00
问题 In ASP.NET MVC 2, you can use this URL and this controller method: GET http://server/controller/get?id=5 public ActionResult Get(int id) { ... } And the ModelBinder will convert the id=5 querystring to id = (int) 5 in the method parameter. However, this won't work: GET http://server/controller/get?idlist=1,2,3,4,5 public ActionResult Get(int[] idlist) { ... } idlist will be null in the parameter. Although the parsing for this is pretty trivial, I was wondering if there is a way to either

ASP.NET MVC UpdateModel empty property

荒凉一梦 提交于 2019-12-09 18:38:34
问题 Given the following Model, public class A { public string Name { get; set; } } public class B { public string Address { get; set; } public A InstanceOfA { get; set; } } View, <%= Html.TextBox("A.Name") %> and Controller UpdateModel<B>(b, collection.ToValueProvider()); my b instance will contain a property of A with an empty string for Name. Is there anyway to have UpdateModel set the A property to null if no value has been entered for name? To clarify, this is a simple case, my real world

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

我的未来我决定 提交于 2019-12-09 17:34:56
问题 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 {