FAQ Database Discussion Community


Model binding space character to char property

c#,asp.net-mvc,model-binding
I have a simple viewmodel with a char property... public char Character1 { get; set; } The default model binding doesn't seem to be converting a space character (" ") into this property, resulting in the following ModelState error... The Character1 field is required. The html input element is created...

Bind Model property to multiple fields

c#,asp.net-mvc,model-binding
I have a model string property: SecurityGroupMapping, which I want to be able to set using one of two lists of strings. The lists are shown based on the selection given in a radio group assigned to a bool. I've looked at this post: MVC 3 Bind Model property to...

Binding select lists to a model in mvc 3

asp.net-mvc,model-binding,selectlist
Looking for some help with binding a select list to the model on form submit. This all works @Html.DropDownListFor(m => m.LectureRoomId, new SelectList(Model.LectureRooms, "Id", "Name"), new { @class = "input-small chosen-select"}) However we are using chosen.js and want to use optgroup to group the drop down items. From what I...

How to create an HtmlHelper extension method that will bind an IEnumerable to a table

asp.net-mvc,html-table,ienumerable,model-binding
This is my view model: public class TaskViewModel{ public int TaskID{get;set;} public IEnumerable<TaskExecutor> Executors{get;set;} } public class TaskExecutor{ public int ExecutorID{get;set;} public string LastName{get;set;} public string FirstName{get;set;} } In my view I have done something like this: <table> @foreach(var item in Model.Executors) { <tr> <td>item.ExecutorID</td> <td>@string.Format("{0} {1}",item.FirstName,item.LastName)</td> </tr> } </table>...

WebAPI model binding complex object with [FromUri] sent via query string

c#,asp.net-web-api,model-binding
I've read a lot of questions surrounding issues as to why model binding is NOT working, but my question is actually the opposite. I'm curious: Why does model binding to the complex object occur in the following GET action, even when no data is provided in the query string that...

Best way to exclude some properties from binding

asp.net-mvc,asp.net-mvc-5,model-binding
One of my classes has some properties that I want model binder to always ignore them. Currently I'm using [Bind(Exclude ="")] in the action methods, like this: public ActionResult Edit([Bind(Exclude = "prop1, prop2, prop3")] BusinessModel model) This class has been used in several action methods. Do I have to manually...

How to configure a One-to-Many relationship in EF

c#,entity-framework,one-to-many,model-binding,junction-table
I have the following model public class PageConfig : Base { // Properties Etc.. public ICollection<Image> ScrollerImages { get; set; } } My approach is to bind using a junction table { PageConfigID, ImageID }. In my model binder i tried the following.. modelBuilder.Entity<PageConfig>() .HasMany(x => x.ScrollerImages) .WithMany() .Map(x =>...

How to model bind from view to controller via nested view

c#,asp.net,asp.net-mvc-4,model-view-controller,model-binding
i cant seem to pass the employee id value from a check box in the Employees.cshtml view to the Delete Post action method. The delete action method returns Delete.cshtml view which renders the Employees.cshtml as that is in an EditorTemplates folder under the Shared folder. When I click submit i...

ASP.NET MVC - How to “reverse” model binding to convert a C# model back to a query string representation

javascript,c#,asp.net-mvc,serialization,model-binding
I have a custom javascript on the client side that I use to build up a querystring and pass over to my asp.net-mvc controller var templateQueryString = BuildTemplate(); $.ajax({ url: '/MyController/Save?' + templateQueryString, type: 'post', dataType: 'json', success: function (data) { } } and on my controller all of the...

IModelBinder: access the raw data of the request

c#,asp.net-mvc,asp.net-mvc-5,model-binding
I am trying to look at the text sent in a POST through the IModelBinder interface. I have something like: public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special content type")) { var data = ??? ...where the ??? should be the text that was sent in the POST....

ASP.NET MVC Model Binding Resource Sequence

asp.net,model-view-controller,model-binding
Just realized that mvc model binder will collect all data from route parameters + query string data + form collection data. And i want to know the sequence of the model binder choosing these resources. In another word, if there are properties (say id, name) with same name in these...

What's the WebApi [FromUri] equivalent in ASP.NET MVC?

asp.net-mvc,model-binding
In WebApi I can decorate a parameter on a controller action with [FromUri] to have the components of the URI 'deserialized', if you will, into a POCO model; aka model-binding. Despite using MVC since 2.0, I've never used it for websites (dunno why). What's the equivalent of it in ASP.NET...

Better Way To Edit A View Model in MVC

asp.net-mvc,viewmodel,model-binding
I actually came up with a working example of how to display (GET) and edit (POST) a view model consisting of three models in MVC. However, my MVC skills are limited and I was looking for suggestions on "the right way" I should be doing this. The part in question...

How can I bind multiple form values into one property?

c#,asp.net,asp.net-mvc-5,model-binding
I have 3 inputs with the name: Field_Day, Field_Month, Field_Year. And a Model that looks like this: public class Model { public string Field { get; set; } } How can I bind those 3 form values into my Field property ("10.10.2015")? Edit: Most answers suggested to change my view...

MVC Default binder not binding a nested class

c#,asp.net-mvc,model-binding
I'm having trouble getting the MVC default binder to serialize a nested object. I've done quite a bit of research to try and figure out what I'm doing wrong, but I'm not seeing it. The model comes in with all nested objects created (courtesy of the constructors, without them nothing...

How to bind model during an action?

c#,asp.net-mvc,asp.net-mvc-5,model-binding
I have a dynamic url processor public ActionResult DynamicUrl(string slug = null) this method works through the slug (btw, what does slug stand for?) and works out if the slug is displaying a product or performing a product search. As part of the product search, I have a page=1 querystring...

Unit Test the BindAttribute for method parameters

c#,asp.net-mvc,unit-testing,model-binding
I am looking to write unit tests to validate my controller while ensuring that the bind properties are setup correctly. With the following method structure, how can I ensure that only the valid fields are passed from a unit test? public ActionResult AddItem([Bind(Include = "ID, Name, Foo, Bar")] ItemViewModel itemData)...

Model Binding to AJAX suddenly not working

c#,jquery,ajax,model-binding
I have an AJAX POST request which is supposed to send a user's search filters off to a controller action and return appropriate results. This was working perfectly, then it just stopped working. I haven't changed a thing, it was working fine, the model was binding exactly as intended, then...

NancyFx bind TimeSpan is not Working json

c#,angularjs,model-binding,nancy
Good Morning (here) I'm using NancyFx and i've a problem when i try to bind a class with TimeSpan Properties, i'm using AngularJs too a json that i use is: { "description": "foo", "scheduleTime": { "days": 0, "hours": 23, "minutes": 36, "seconds": 10, "milliseconds": 0 } }; My C# Class...

asp.net 5: Bind attribute with Include parameter - include is not a valid named attribute argument

attributes,model-binding,asp.net-5,asp.net-mvc-6
I write code from the article and and there is: public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik pracownik) { blablablab } I want to compile but it shows error. include is not a valid named attribute argument. But in the internet I saw a code similar to my code. Someone explain to me...

Customize mapping request parameters and fields inside the DTO ?

spring,spring-mvc,model-binding,spring-mvc-initbinders
I have the following class: public class MyDTO { private String kiosk; ... } and following url: http://localhost:1234/mvc/controllerUrl?kiosk=false and following controller method: @RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON) @ResponseBody public ResponseEntity<List<?>> getRequestSupportKludge(final MyDTO myDTO, BindingResult bindingResult) { ... } Now it is working nice and boolean field resolves properly. Now...

Using Json.NET for JSON Model Binding

json,asp.net-mvc,json.net,model-binding
I have a method being posted to via AJAX with the following header: public JsonResult GetDocuments(string searchTerm, SortRequest sort) The SortRequest object is defined as follows: [DataContract] public class SortRequest { [DataMember(Name = "field")] public string Field { get; set; } [DataMember(Name = "dir")] public string Direction { get; set;...

Using ng-click inside np-repeat

angularjs,angularjs-scope,angularjs-ng-repeat,model-binding,angularjs-ng-click
I'm trying to implement a shortlisting functionality in a case where I'm using ng-click inside ng-repeat. While the $index is being displayed correctly outside the ng-click, $index is only the index of the last object in the JSON array in all the cases where the html is generated using ng-repeat....

Model binding for float fails

asp.net-mvc,model-binding
I'm trying to send window's position to the server. But model binding for top always fails. My action method is as below: public void winClosed(Window position) And the window model: public class Window { public decimal Left { get; set; } public double Top { get; set; } } In...

my model binding is working even If I didn't show it in html

c#,asp.net,asp.net-mvc,razor,model-binding
I have a form that can be reached by different ways. In the URL, if user comes with ?data=johndoe (example), I didn't want to deal with another property, so I didn't show it to user and I don't care of this property. Because I use the same form and the...

How to pass (access) ID inside rules method in Laravel 5?

php,laravel,laravel-5,model-binding
I'm populating my form with user's data using form model binding. {!! Form::model($user, ['route' => ['user_update', $user->id]]) !!} Works perfectly. When I hit "Submit" I enter my UpdateUserRequest where I validate my input against some rules. In this case I want to keep unique e-mail address but of course we...

Why Html.TextBox with array index prefixes , can not read model data that are constructed as a Collection

c#,asp.net-mvc-4,razor,asp.net-mvc-5,model-binding
I have two model classes; skill & skill Level. where each Skill can have many SkilLevels:- public partial class Skill { public Skill() { this.SkillLevels = new HashSet<SkillLevel>(); this.SkillLevelStaffs = new HashSet<SkillLevelStaff>(); this.SkillVersionHistories = new HashSet<SkillVersionHistory>(); this.Customers = new HashSet<Customer>(); this.LinkToKBs = new HashSet<LinkToKB>(); } public int SkillID { get;...

ASP.NET MVC application throwing system.nullreferenceexception - modelbinding to list [duplicate]

c#,asp.net,asp.net-mvc,nullreferenceexception,model-binding
This question already has an answer here: What is a NullReferenceException and how do I fix it? 17 answers I have an asp.net application that has an index view that users can modify and save. However, when I click the save button I get the error: Object reference not...

ASP.NET MVC model binding with serialize

jquery,json,asp.net-mvc,model-binding
I'm using serialize and JSON.stringify methods to make an Ajax call to my ASP.NET MVC application. MVC is unable to bind the model. This is my JS code and strongly-typed view: <script> function saveDetails() { jsonObj = $('#rdform').serialize(); $.ajax({ method: "POST", url: "R/SaveDetail", contentType: 'application/json; charset=utf-8', dataType: "json", data: JSON.stringify(jsonObj)...

Issue updating modelbinding radio button in AngularJS

angularjs,radio-button,model-binding
I need help to get the last right in my form. It's a simple page where I have a question with some answers. One of the answers can be set to correct, this is not true always. But one question can only have one correct answer, so I use radio...

WebAPI modelbinding to complex type containing list of complex types - from body

asp.net,angularjs,asp.net-web-api,model-binding,complextype
I've read and tried this and this and keep going around in circles. Edit: The client is sending the model it received, the server builds the complex types as per the classes listed below and sends it to the client, the client sends the exact same model back but model...

Can I use Laravel 5 to clone a select box and still use form model binding?

php,laravel-5,jquery-select2,model-binding,jquery-cloneya
(Please excuse the multifaceted question!) Basically the question is; how can I use Laravel 5 functionality to clone a select box, save that data, and( as a bonus) when returning to the form have the data displayed using form model binding. So if there is one work experience "group" (being...

MVC. Bind TextBoxFor to a Field of type object

asp.net,asp.net-mvc,model-binding
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...

ASP.Net MVC Model binding a [Serializable] class

c#,asp.net-mvc,asp.net-web-api,model-binding
I have this class [Serializable] public class MyObject { // properties omitted } And this WebAPI controller method: [HttpPost] [ResponseType(typeof(string))] public IHttpActionResult SetMyObject(MyObject o) { // process object } But it fails to model bind to the MyObject class. The object o in the controller method is completely empty, every...

Model binding to Dictionary in Nancy

c#,model-binding,nancy
I can't bind JSON to Dictionary<string,string> in Nancy. This route: Get["testGet"] = _ => { var dictionary = new Dictionary<string, string> { {"hello", "world"}, {"foo", "bar"} }; return Response.AsJson(dictionary); }; returns the following JSON, as expected: { "hello": "world", "foo": "bar" } When I try and post this exact JSON...

MediaFormatter or ModelBinder for web api PUT method

asp.net,asp.net-mvc,asp.net-web-api,asp.net-web-api2,model-binding
I have a PUT method in web api which accepts a JSON data and a route data as follows. [Route("api/v1/Orders/{orderId}/active")] public HttpResponseMessage Put(Guid? orderId,List<ActiveRequest> activeRequests) { } public class ActiveRequest { public int Id { get; set; } public bool IsActive { get; set; } } Now is it possible...