MVC Ajax Int32 error - ajax

Hi when I try to do an Ajax post to my controller I keep getting this message:
The parameters dictionary contains a
null entry for parameter 'id' of
non-nullable type 'System.Int32' for
method 'System.Web.Mvc.JsonResult
GetContactsByDepartment(Int32)' in
'Intranet.Controllers.MyController'.
An optional parameter must be a
reference type, a nullable type, or be
declared as an optional parameter.
The method head looks like this: public JsonResult GetContactsByDepartment(int id)
What am I missing? I have ensured that the id is being passed through my Jquery ajax call.

Try to rename id to for example DepartmentID.
Probably an issue with your registered routes.

Related

problem with the payload when saving a record

when I try to add\change the value of a custom lookup and then save it, I get this error:
Exception Message: Error identified in Payload provided by the user for Entity :'', For more information on this error please follow this help link https://go.microsoft.com/fwlink/?linkid=2195293 ----> InnerException : Microsoft.OData.ODataException: An undeclared property 'ey_org_unit_hr_id' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.
at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.ReadUndeclaredProperty(IODataJsonLightReaderResourceState resourceState, String propertyName, Boolean propertyWithValue)
at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.ReadPropertyWithoutValue(IODataJsonLightReaderResourceState resourceState, String propertyName)
at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.<>c__DisplayClass9_0.<ReadResourceContent>b__0(PropertyParsingResult propertyParsingResult, String propertyName)
at Microsoft.OData.JsonLight.ODataJsonLightDeserializer.ProcessProperty(PropertyAndAnnotationCollector propertyAndAnnotationCollector, Func`2 readPropertyAnnotationValue, Action`2 handleProperty)
at Microsoft.OData.JsonLight.ODataJsonLightResourceDeserializer.ReadResourceContent(IODataJsonLightReaderResourceState resourceState)
at Microsoft.OData.JsonLight.ODataJsonLightReader.StartReadingResource()
at Microsoft.OData.JsonLight.ODataJsonLightReader.ReadResourceSetItemStart(PropertyAndAnnotationCollector propertyAndAnnotationCollector, SelectedPropertiesNode selectedProperties)
at Microsoft.OData.JsonLight.ODataJsonLightReader.ReadAtStartImplementationSynchronously(PropertyAndAnnotationCollector propertyAndAnnotationCollector)
at Microsoft.OData.ODataReaderCore.ReadImplementation()
at Microsoft.OData.ODataReaderCore.InterceptException[T](Func`1 action)
at System.Web.OData.Formatter.Deserialization.ODataReaderExtensions.ReadResourceOrResourceSet(ODataReader reader)
at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger).
ErrorCode: -2147185383
HexErrorCode: 0x80048d19
the field's logical and schema name is "ey_org_unit_hr_id" (both of them are identical).
I opened the post request that the form sends and this is what I found:
{"ey_org_unit_hr_id#odata.bind":"/ey_businessunits(91293b20-afb7-e611-bec8-0050568c00dc)",
"ey_org_unit_hr_id#OData.Community.Display.V1.FormattedValue":null,
"ey_s_name":"test555",
"ey_b_is_doctor_house":false,
"ey_b_is_route_service_requests":false,
"ey_b_real_branch":false,
"statuscode":1,
"statecode":0}
then I opened the metadata and found out that the NavigationalProperty Name of this field is different than what is used in the post request (ey_org_unit_hr_id is used in the request but the navigational property name is ey_ey_businessunit_ey_branch_hr which is also the name of the relationship).
Photo of the navigation property name
so i tried to send a post request using postman whith the currect NavigationProperty Name and it worked without an error.
Here is the request:
{"ey_ey_businessunit_ey_branch_hr#odata.bind":"/ey_businessunits(91293b20-afb7-e611-bec8-0050568c00dc)",
"ey_ey_businessunit_ey_branch_hr#OData.Community.Display.V1.FormattedValue":null,
"ey_s_name":"test555",
"ey_b_is_doctor_house":false,
"ey_b_is_route_service_requests":false,
"ey_b_real_branch":false,
"statuscode":1,
"statecode":0}
I thought about changing the navigational property name to what is used in the original post request but I don't know if I can change the metadata.
Try this property name: _ey_org_unit_hr_id_value.
You should check your error code at the below link.
https://learn.microsoft.com/en-us/power-apps/developer/data-platform/reference/web-service-error-codes
AFAIK, the 0x80048d19 code will be exposed by sending request with invalid data.
In my case, I got this error code when i sent request without header "Content-Type: application/json"
I hope this can be helpful to you

How to return with error if DateTime argument is not a parsable Date?

Context
I have an ASP MVC WebApi controller. My action method has an optional DateTime? parameter:
public MyType Query(DateTime? myDate = null)
The parameter binding parses dates correctly, however if I sending a request with invalid date, it becomes null, so I can not distinguish between intentionally omitted optional parameters and bad requests.
Question
I would like to return to bad requests (invalid date) with error. Can I solve this using the existing parameter binding mechanism, or do I have to declare myDate as string and parse it in the action method code myself?

Why does #GetMapping method return request param while sending response?

I get request params as POJO and do nothing in method but in jsp shows parameter which I get from request.Why does method return my object?
Additionally, when I use primitive type or String, It doesn't return object and works perfectly
Controller
#GetMapping("/ULD_details")
public String ULD_detailGet(ActionError ID){
return "ULD_detail";
JSP
<tr>
<td >ULD id</td>
<td>${actionError.ID}</td>
</tr>
Link
http://localhost:8080/UCM-controller/ULD_details?ID=1145
It doesn't return your object. It returns the String "ULD_detail", which is the name of the view to execute.
This view is executed, and finds an actionError bean in the request attributes (i.e. in the model), because the Spring documentation about handler methods arguments says:
Any other argument
If a method argument is not matched to any of the above, by default it is resolved as an #RequestParam if it is a simple type, as determined by BeanUtils#isSimpleProperty, or as an #ModelAttribute otherwise.
And the documentation of ModelAttribute says:
The default model attribute name is inferred from the declared attribute type (i.e. the method parameter type or method return type), based on the non-qualified class name: e.g. "orderAddress" for class "mypackage.OrderAddress"

Bind int? property to null if value is a non-numeric, non-empty string

I have a viewmodel with a property of type int?.
If the send value is an integer or is empty it works fine. When the value is a string an exception is thrown 'Could not convert string to integer'
Would it be possible to change the behavior of the binding that instead of throwing a error it would bind the null value ?
I've created a modelbinder class for typeof(int?), but the ModeBind function doesn't get called. I guess the validation happening before the model binding.
Any ideas ?
When you say you have a view model are you referring to the parameter that is being passed to your action method? E.g.
public Foo Bar(MyViewModel vm)
If so then your modelbinder should be of type MyViewModel. You can additionally use a ValueProvider to extract an int from a random string to override the default value provider functionality.

MVC - Passing a Type as an action parameter

Can I pass a parameter of Type to the Action method in MVC via a HTML.ActionLink().
e.g.
public ActionResult Get(Type containerType, string name)
{
throw new NotImplementedException("vkag");
}
<%=Html.ActionLink(flow.Source.Name, "Get", new { containerType = typeof(Worksheet), name = flow.Source.Name})%>
For some reason the type is always null. I also can't pass back a base type to allow for subtypes to be passed in it's place.
I don't know whether this is a limitation of the HTML ActionLink Helper or MVC.
I can't pass an Interface, I specify a base type parameter and use sub type as an argument in the place of a base type parameter, and I can't seem to pass a Type back. I'm pretty stuck here.
Is there another HTML Helper that will help?
Can I do anything with model binding?
Can I modify the state of my model during an ActionLink invocation before control is passed to my action method?
Kind a stuck here. Any help would be gratefully appreciated.
Thanks
The types you can pass into Routes must be passable via a URL if you think about it.
You cannot pass a Type as it would have to be serialised. Why not pass the containerType as a string and convert it back to a Type in the action?

Resources