c# - Is it possible to have one parameter of type object in a controller's action accepting int, date, float or string from json -


I have a collection of key-value pairs, where part of the value can be any type of string (C #) There is an optional key for a class called that-time offset, long, float, boolean key-value pair component:

  components of public class {public long id {get; Set;} public string key {get; Set; } // Here other properties ... public object values ​​{get; Set;}}  

The user enters the value part in an input bound with knockoutjs with an input, and I use the key with this controller action and the corresponding component example with that value Expected method to find (MVC4):

Public JsonResult GetComponent (string compkey, object compValue) {var comp = Database.FindComponentValue (compKey, compValue); Return it back. Jason (Comp, Jason Aref Behavior, Elove Gate); }

which I present in the function of the knockout view model in this way:

  self.findComponent = function (component) {// component is Ko .observable () generated on atom with ko.mapping.fromJS () / var compdata = ko.mapping.toJS (component); $ ("Mac Controller / GetCononant", {compClasskey: component.FullCode (), compValue: component.Value ()}, self.validateComponent); };  

validateComponent is a function that shows an icon properly, then the component is found.

Now, if a component.Value () is the string value, MyController.GetComponent receives an array of values ​​with the value in the first position (compValue [0]), but the compValue parameter As the declaration string works:

  public JsonResult GetProductComponent (string compClassKey, string compValue) {...}  

But to do this In order to be able to accept the declaration of the system in a different way in order to me:

  Nick JsonResult GetProductComponent (? String compClassKey, String compValueString, DatetimeOffset compValueDateTimeOffset, bool compValueBoolean, long compValueLong) {...}  

Another way should be compValue of type string and type it like this Other parameters of the string should also be in

Is this a solution or is it only a parameter of a type of object and I am making a mistake because of which I am not seeing?

Your basic issue is that you are pushing data payloads as items on query string, not That as Jason Payload In this scenario, the query string value provider will be responsible for popularizing the parameters of the method.

All queries are considered as an array by the string key value provider. If you specify a data type and the value of the key is 1 entry then the provider will be leveling the array to a single value.

In your case you specify a object datatype, the provider gives you an array of 1 entry.

You need to switch from $ to fix this. $ Ajax, provide some additional parameters and force the data payload to a JSON string.

  $ Ajax ({url: "MyController / GetComponent", Type: 'POST', Content Type: 'Application / Jason', Datatype: 'JSON' Data: JSON. String ({compClasskey: component .FullCode (), compValue: component Value ()})});  

Now the MVC application uses the JSON value provider (as the ContentType is now changed to application / json ) instead of the query string provider and compValue parameter

As one side: IMHO should not request a JSON document through a GET request, it is still an attack vector, unless you 100% Can not guarantee IE10 +, Chrome 27+ or Firefox 21+


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -