String property not deserialized - javascript

I don't understand why only the string property Search.Value is not being deserialized in my ASP.NET MVC5 Controller. Please see this:
The Json-structure sent from the Client:
{
"draw":1,
// ...
"start":0,
"length":50,
"search":{
"value":"This is always null in my controller",
"regex":false
}
}
The model I have server-side:
public class AsyncDataTableRequest
{
public int Draw { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public Search Search { get; set; }
public string Value { get; set; }
}
public class Search
{
public string Value { get; set; }
public bool Regex { get; set; }
}
The controller where I would like to do something with Search.Value:
public JToken AsyncLogFetching(AsyncDataTableRequest req)
{
// req.Search.Value is null here, all other properties seem correct
...
}
Thank you for any help!
Edit:
For a sample search with "NewYork", this the request from the tab "request header" in IE Developer tools:
GET /Log/AsyncLogFetching?draw=3&start=0&length=50&search%5Bvalue%5D=NewYork&search%5Bregex%5D=false&_=1438350434912 HTTP/1.1
The tab "request text" in IE Developer tools says "No data to display".
This is the snippet that does the GET-Request, it's copy & pasted from the jQuery DataTables Pipelining example:
settings.jqXHR = $.ajax({
"type": conf.method, // GET
"url": conf.url,
"data": request,
"dataType": "json",
"contentType": "application/json",
"cache": false,
"success": function (json) {
// ...
}
});

try to use JSON.stringify(yourObject) for send the data from client to controller
please take the reference of following
How to send nested json object to mvc controller using ajax

Related

How to upload files with additional file data in asp.net core

I am trying to upload multiple files (documents) with additional data per file.
Ie. I would like to upload multiple documents like this:
[{
documentFile: file
documentVersion: 2,
documentOperation: "createNew"
...
},
{
documentFile: file2.doc
documentVersion: 1,
documentOperation: "createNew"
...
}]
And ideally would like that information to be populated in the model but this is not a requirement.
I am using Aurelia Fetch client to send data using FormData.
Is this possible?
If your model is like:
public class Documents
{
public IFormFile documentFile { get; set; }
public string documentVersion { get; set; }
public string documentOperation { get; set; }
}
You can see my test.
Action:
[HttpPost]
public IActionResult Demo(List<Documents> documents)
{
//...
}
Send data by postman:
Result:

How do I access data returned from a controller invoked by an ajax call in an MVC View

I have an MVC 5 view with the following JavaScript which is getting an error after invoking an action method on a controller:
<script type="text/javascript">
$('#ddlvendors').change(function () {
var descHtml = "";
var vendorId = $(this).val();
$.ajax(
{
type: "POST",
data: { vendorId: vendorId },
url: '#Url.Action("PurchaseOrderVendor")',
datatype: "json",
success: function (aVendorObject) {
alert(aVendorObject.Name);
},
error: function (req, status, error) {
alert(error);
}
});
});
</script>
The controller action method is as follows:
[HttpPost]
public ActionResult PurchaseOrderVendor( int vendorId)
{
Vendor aVendor=VendorServices.GetVendor(vendorId);
return Json(aVendor);
}
The vendor object being returned is as follows:
public class VendorViewModel
{
public int VendorId { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Description { get; set; }
public string ContactName { get; set; }
public string Phone { get; set; }
}
Using the Visual Studio 2017 debugger I can see the vendor data is correctly getting filled on the controller side. If I don't try to access any member data the ajax call completes without an error. However, when I try to access any part coming in as aVendorObject such as aVendorObject.Name I get a browser error saying "Internal Server Error". How can I fix my success handler in the ajax call so I can access members of the Vendor object being returned from the controller?
Thanks in advance.
The LazyLoading feature of EntityFramework 6 did not have the Vendor object available when it was needed. I added a line in the VendorServices class to turn off LazyLoading which fixed the problem. Below is the method where I turn off LazyLoading for this particular VendorService method:
static public Vendor GetVendor(int aVendorId)
{
Vendor vendor;
using (RgmSiteDAL RgmSiteDALCtx = new RgmSiteDAL())
{
//** Now setting LazyLoadingEnabled to false.
RgmSiteDALCtx.Configuration.LazyLoadingEnabled = false;
vendor = RgmSiteDALCtx.Vendor
.Where(v => v.VendorId == aVendorId)
.FirstOrDefault<Vendor>();
}
return vendor;
}
I found out that I can also fix the problem if I include any navigation properties defined with the Vendor entity as in the following:
vendor = RgmSiteDALCtx.Vendor
.Where(v => v.VendorId == aVendorId)
.Include(v => v.PurchaseOrder) //because is in Vendor navigation property
.FirstOrDefault<Vendor>();
For my situation I will stick with the first solution which sets LazyLoadingEnabled to false. This is because the option to generate my entities from my existing database setup my PurchaseOrder table as a navigation property for my Vendor which I believe is incorrect. My Vendor object does not need anything from my PurchaseOrder entity when I query for Vendor information. I will look into removing the PurchaseOrder navigation property from my Vendor entity because I believe it was incorrectly setup by the create entities from database tool in Visual Studio.

Send JS object to C# Controller using Ajax

I am trying to send multiple objects in js to a controller in C# using an Ajax call.
I have a object in C# called "Person"
which is the next :
public class Person
{
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
and I have the same object In JavaScript.
Then create two persons and I send to the controller.
This is the Ajax call
$.ajax({
url: baseUrl + "/controller/TestPeople",
type: "POST",
data: {
people: people
},
success: function (resp) {
alert("ok");
}
});
This is the post
people[0][Title]:"Mr."
people[0][FirstName]:"fname1"
people[0][LastName]:"Lname1"
people[0][Age]:23
people[1][Title]:"Mr."
people[1][FirstName]:"fname2"
people[1][LastName]:"Lname2"
people[1][Age]:25
but when i receive it in the controller, everything is null
public string TestPeople(Person[] people){
//some code
}
the controller knows that there are 2 people but all the information inside is null.
Any idea why?
To "solve" the problem i change the controller to use FormCollection and it is working, but i would like to know why the other is not working.
Thanks for all.
Try with:
data : JSON.stringify(peopleArray)
In your controller try:
public string Get(Person[] people){
//some code
}

How to handle parameters on C# server side sent from jQuery POST?

So I'm trying to send the following data from jQuery on the server side like so:
var fd = new FormData();
fd.append('name', 'tyler');
fd.append('hello', 'world');
$.post('/NMISProduct/Index', { submitData: fd}, function(returnedData) {
console.log(returnedData);
}, 'json');
How do I handle this on the server side? Here is what I have, which I'm sure is very wrong:
[HttpPost]
public string Index(string submitData)
{
return submitData;
}
I just want to return what I send to C# back to jQuery so I know it got there. What am I doing wrong?
Your current approach ties you to FormData() and it doesn't take advantage of JSON.Net which is happy and eager to deserialize your object so that you can consume it.
If you truly want to test "full-loop", deserialize to a strongly typed object and return it back to the client as serialized json, building out the matching object on the client instead of using FormData().
$.post('/NMISProduct/Index', { name: 'tyler',hello: 'world' }, function(data) {
console.log(data);
});
[HttpPost]
public ActionResult Index(FormData submitData)
{
return Json(submitData);
}
public class FormData
{
public string Name { get; set; }
public string Hello { get; set; }
}

Some JSON fields showing up as NULL when hitting MVC Controller method

After submitting an ajax call, I have confirmed in the request header that all the json fields have a value. What I do not understand is that when I debug on the server side, only the DateEntered field has a value (parsed into a DateTime object). When I put the same information in using Postman, it works just fine on the server (all the fields have a value).
Controller method:
[HttpPost]
public JsonResult SaveJobApplication(ApplicationFormObject jobApplicationObject)
{
return Json(_jobApplicationController.SaveJobApplication(jobApplicationObject), JsonRequestBehavior.AllowGet);
}
Ajax call:
$.ajax({
url: 'http://myurl.com/SaveJobApplication',
data: JSON.stringify(applicationInfo),
contentType: 'application/json',
type: 'POST'
}).done(function(message) {
// do some stuff
});
Here is the context:
The ajax request is coming from a different origin 'http://somedomain.com' than my server origin 'http://myserverdomain.com'. I have implemented CORS in the Web.Config file like so:
...
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
...
This has been tested and it is working fine (My server is able to grant ajax requests from other origins).
The json object that I am submitting is like this:
var applicationInfo = {
FirstObject: {
'NameFirst': 'blah blah',
'NameLast': 'blah blah blah',
'DateEntered': '2012-5-5'
},
SecondObject: {
'LastJob': 'blah blah',
'ReasonQuit': 'blah blah blah'
}
}
The class/objects on the server are like this:
// Object that ajax is utilizing
public class ApplicationFormObject
{
public FirstObject FirstObject { get; set; }
public SecondObject SecondObject { get; set; }
}
public class FirstObject
{
public string NameFirst { get; set; }
public string NameLast { get; set; }
public DateTime? DateEntered{ get; set; }
}
public class SecondObject
{
public string LastJob { get; set; }
public string ReasonQuit { get; set; }
}
Important Information
The json object that I am sending is created dynamically. I actually loop through the form elements via ajax .each. For each element I am adding it to an object like so:
var applicationInfo = {};
$('form input, form select').each(function() {
var that = $(this);
if(isValid(that)) {
applicationInfo["'" + that.attr('id') + "'"] = that.val();
}
});
When I look at the request header, it shows the JSON as being in such format:
{
...
'demoObject': "Value of demo object"
...
}
However, when I test it on Postman, the object is not created dynamically. I explicitly fill in the json object one by one for test data. When I look at the request header for this method, it shows the JSON as being in such format:
{
...
demoObject: "Value of demo object"
...
}
the difference being that the second example does not have single quotes, and the first one does. Hope this information makes sense. Please let me know how I can clarify.
You are passing an invalid Model.
Modify your code as follows :
[HttpPost]
public JsonResult SaveJobApplication(JobApplication jobApplication)
{
return Json(_jobApplicationController.SaveJobApplication(jobApplicationObject), JsonRequestBehavior.AllowGet);
}

Categories