model-binding

ModelBindingContext.ValueProvider.GetValue(key) always returns null

♀尐吖头ヾ 提交于 2019-12-11 07:29:25
问题 I'm using AngularJS to manipulate a fairly complex parent object with children that need to behave quite differently server-side. Based on this answer, which appears pretty solid, I've created the test case below. The issue I'm running into is that whenever I enter the CreateModel function, any call to bindingContext.ValueProvider.GetValue(key) returns null. I've checked all values in the debugger. The object type appears to be loaded, but no values have yet been bound. My Models: public

Cannot get my html input array to serialize into a List<string> in Asp.Net mvc

痞子三分冷 提交于 2019-12-11 07:16:26
问题 I am attempting to implement a tagging system into my asp.net MVC project. When a user edits or adds a task, they can add any amount of tags they want before submitting. I am using the Jquery Tagit plugin, so when a user adds a new tag an input field is created that looks like: <input type="hidden" style="display:none;" value="tag1" name="Tags[]"> When the user presses the submit button after adding a few tags, the browser sends the following querystring to the server (retrieved via fiddler):

default model binding does not work in case when model contains a model

痞子三分冷 提交于 2019-12-11 06:38:30
问题 Here are my model. public class InfoModel { public NameModel Name { get; set; } public string Phone { get; set; } } public class NameModel { public string FirstName { get; set; } public string LastName { get; set; } public NameModel(string first, string last) { this.FirstName = first; this.LastName = last; } } Then I have a partial View just for displaying names as follows @model MyTestApp.Models.NameModel @Html.LabelFor( m => m.LastName) @Html.TextBoxFor( m => m.LastName) <br /> @Html

Handle multiple binding errors from ModelBindingException in NancyFX when binding to JSON in Request.Body

余生长醉 提交于 2019-12-11 06:17:19
问题 I have a post route that accepts some JSON payload in the request body. Post["/myroute/}"] = _ => { try { var model = this.Bind<MyModel>(); } catch (ModelBindingException e) { //PropertyBindException list is empty here, //so only the first exception can be handled... } } If there are multiple invalid data types (i.e. if there are several int properties defined in MyModel, and a user posts strings for those properties), I would like pass back a nice list of these errors, similar to how would

MVC 3 Bind Model property to 2 fields (e.g. Other Title)

泄露秘密 提交于 2019-12-11 05:28:16
问题 I'm trying to achieve a very common scenario whereas given a list of options to choose from, the last one says "Other" and when selected the user is presented with the input field to specify what "other" is. In my case, it's a list of Person's titles: public List<string> TitleList { get { return new List<string> { "Mr", "Mrs", "Miss", "Dr", "Other" }; } } and what I'm trying to do is this: @Html.DropDownListFor(m => m.Title, new SelectList(Model.TitleList), "Please select...") @Html

How instantiate a concrete class in init binder?

限于喜欢 提交于 2019-12-11 05:08:54
问题 I have to bind an abstract class to my controller request handler method. Before I instantiate the concrete class with @ModelAttribute annotated method: @ModelAttribute("obj") public AbstractObh getObj(final HttpServletRequest request){ return AbstractObj.getInstance(myType); } But now I try to do that without the @ModelAttribute annotated method, beacause every call to controller trigger model attribute annotated method too. So I try to get concrete class with InitBinder and a custom editor

IEnumerable is null when I expect values

↘锁芯ラ 提交于 2019-12-11 04:57:26
问题 I am using the approach in this article to return a list of objects that are posted to my action. My method looks like: // // POST: /LeaveRequest/Create [Authorize, HttpPost] public ActionResult Create(LeaveRequest leaveRequest, IEnumerable<DayRequested> requestedDays) { return RedirectToAction("Index", lrRepository.GetLeaveRequests(472940821)); } leaveRequest has the data that I expect. requestedDays contains the number of rows I expect, but all of the rows contain null in each field. Any

How to override global ModelBinder for specific post action mvc5

你离开我真会死。 提交于 2019-12-11 04:42:23
问题 I have applied a global ModelBinder for int arrays as below: public class IntArrayModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (string.IsNullOrEmpty(value?.AttemptedValue)) { return null; } try { return value .AttemptedValue .Split(',') .Select(int.Parse) .ToArray(); } catch (Exception e) { Console.WriteLine(e);

Should I use a view model for just two objects?

一个人想着一个人 提交于 2019-12-11 04:22:24
问题 Lets say I have a view that accepts a Person object. Has three properties, FirstName , LastName , Age Now lets say I add another textbox field that's not part of the object. I don't need the value of the textbox, its just populated with data that's for the user. When you edit the fields and post the Person to the controller, lets assume there is a validation problem so you return the Person object back with Errors The problem is now the additional textbox has lost it's value since its not

ASP.NET MVC ViewModel binded decimal empty when using floating point numbers

老子叫甜甜 提交于 2019-12-11 03:14:36
问题 I created a ViewModel object that has a decimal field containing a price. When I post that to my controller, this is what happens: Enter "15" -> ok! Controller receives 15. Enter "15.00" -> not ok! Controller receives a 'null' field. Enter "15,00" -> validation error because the field should be formatted with a period (I just stick to one formatting type to avoid complexity for the time being). Enter "15.00M" -> validation error, probably because it's not considered to be a number. How do I