Posting JavaScript objects with Ajax and ASP.NET MVC - arrays

How can I post a list from view to controller using ajax? I have this code in client side:
$(".js-save").click(function (e) {
e.preventDefault();
var button = $(this);
var lstERegistroVenta = [];
var ERegistroVenta = new Object();
ERegistroVenta.IdProducto = 1;
ERegistroVenta.IdInventario = 2;
ERegistroVenta.MontoProducto = 12.5;
ERegistroVenta.CantidadProducto = 1;
ERegistroVenta.MontoTotalSinIGV = 20.5
ERegistroVenta.MontoTotal = 23.5
lstERegistroVenta.push(ERegistroVenta);
$.ajax({
dataType: 'json',
type: 'POST',
url: "/API/Inventario/Venta/1",
data: JSON.stringify({ lstERegistroVenta: lstERegistroVenta }),
success: function () {
toastr.success("Correcto");
},
error: function () {
toastr.error("Error");
}
});
});
When I try to pass the data I receive only empty list. In server side I have this API
[HttpPost]
[Route("API/Inventario/{Venta}/{IdProducto}")]
public IHttpActionResult AsignarProducto(int IdProducto,List<ERegistroVenta> lstERegistroVenta)
{
return Ok();
}
public class ERegistroVenta
{
public int IdProducto { get; set; }
public int IdInventario { get; set; }
public double MontoProducto { get; set; }
public int CantidadProducto { get; set; }
public double MontoTotalSinIGV { get; set; }
public double MontoTotal { get; set; }
}

First of all, I wouldn't suggest calling an API method directly from a View. Instead, call a controller method which should internally call the API method. The code for this would look something like below.
$.ajax({
dataType: 'json',
type: 'POST',
url: "/TestController/TestMethod",
data: { "IdProducto" : "1", "lstERegistroVenta": lstERegistroVenta },
success: function () {
toastr.success("Correcto");
},
error: function () {
toastr.error("Error");
}
});
[HttpPost]
public void TestMethod(int IdProducto,List<ERegistroVenta> lstERegistroVenta)
{
// Your Logic Here
}

I found a solution. Maybe my mistake was that I didn't send an anonymous list of objects.
Take a look to this:https://kwilson.io/blog/post-an-array-of-objects-to-webapi-using-jquery/
My solution below:
$(".js-save").click(function (e) {
e.preventDefault();
var button = $(this);
var lstERegistroVenta = [];
var ERegistroVenta = new Object();
ERegistroVenta.IdProducto = 1;
ERegistroVenta.IdInventario = 2;
ERegistroVenta.MontoProducto = 12.5;
ERegistroVenta.CantidadProducto = 1;
ERegistroVenta.MontoTotalSinIGV = 20.5
ERegistroVenta.MontoTotal = 23.5
lstERegistroVenta.push(ERegistroVenta);
$.ajax({
dataType: 'json',
method: 'POST',
url: "/API/Inventario/Venta/1",
data: { '': lstERegistroVenta },
success: function () {
toastr.success("Correcto");
},
error: function () {
toastr.error("Error");
}
});
});
And in server side:
[HttpPost]
[Route("API/Inventario/{Venta}/{IdProducto}")]
public IHttpActionResult AsignarProducto(int IdProducto,List<ERegistroVenta> lstERegistroVenta)
{
// Logic Here
}
Hope this helps to another user.

Related

How do I upload files with several records using an Angular-js http call to an MVC action method?

In design, I have multi records with one file upload in tabular form.
in every ng-change of the file, I am getting the file and storing it into a global variable like below.
app.controller('AngController', function ($http, $scope) {
$scope.EqFiles = '';
$scope.UploadFiles = function (files) {
$scope.EqFiles = files;
};
on every 'Add' action (plus button) I am pulling all values into one array like below
$scope.SystemAccesories = [];
$scope.Add = function () {
var systemdetail = {};
systemdetail.SystemAcsId = 0;
systemdetail.AcsName = $scope.txtAccessoryName;
.
.
.
systemdetail.Remarks = $scope.txtSysRemarks;
if ($scope.EqFiles.length != 0) {
systemdetail.ManualFile = $scope.EqFiles;
systemdetail.IsManualFileAvailable = true;
}
else {
systemdetail.IsManualFileAvailable = false;
}
$scope.SystemAccesories.push(systemdetail);
};
at final I am sending it to the MVC controller's method
$http({
method: 'POST',
url: '/Equipment/UpdateEquipment',
data: { EquipmentAllFields: scope.SystemAccesories },
headers: { 'content-type': 'application/json' }
}).then(function (response) {
});
with the below parameters.
public class SystemAccessories
{
public int SystemAcsId { get; set; }
public string AcsName { get; set; }
.
.
.
public string Remarks { get; set; }
public Nullable<bool> IsManualFileAvailable { get; set; }
public HttpPostedFileBase ManualFile { get; set; }
}
on c# side getting all text values as expected but all 'ManualFile' parameters getting null values.
so how to catch it then I can store with exact related data.
in every ng-change of the file, I am getting the file and storing it into a global variable like below in the form of Base64 Serialization.
$scope.UploadSysFiles = function (event, sysAccId, rowIndex) {
var file = event.target.files;
if (file[0].size < 5000000) {
var reader = new FileReader();
reader.readAsDataURL(file[0]);
reader.onload = () => {
$scope.SysFileByteCode = reader.result;
};
cntSysMan = file.length;
$scope.SysFiles = file[0];
setTimeout(function () {
if (sysAccId === 0 && rowIndex !== -1) {
if (cntSysMan != 0) {
$scope.editedEquipmentList.SystemAccesories[rowIndex].ManualFile = $scope.SysFileByteCode;
}
}
else if (sysAccId !== 0 && rowIndex !== -1) {
if (cntSysMan != 0) {
if ($window.confirm("Do you really like to replace the file '" + $scope.editedEquipmentList.SystemAccesories[rowIndex].FileName + "' by '" + $scope.SysFiles.name + "' ?")) {
$scope.editedEquipmentList.SystemAccesories[rowIndex].ManualFile = $scope.SysFileByteCode;
}
}
}
}, 3000);
}
else {
alert('File size exceeded');
}
};
set time out to get the file because after getting the image to converting it to base64 and on-load event fire.
at final I am sending it to the MVC controller's method
$http({
method: 'POST',
url: '/Equipment/UpdateEquipment',
data: { EquipmentAllFields: scope.SystemAccesories },
headers: { 'content-type': 'application/json' }
}).then(function (response) {
});
MVC Controller
[HttpPost]
public ActionResult UpdateEquipment(EquipmentAllFields objEquipmentAllFields)
{.
.
.
foreach (var systemAcs in objEquipmentAllFields.objSystemAcs)
{
string removedAcsUnwantedString = systemAcs.ManualFile.Substring(systemAcs.ManualFile.LastIndexOf(',') + 1);
var base64AcsEncodedBytes = Convert.FromBase64String(removedAcsUnwantedString);
System.IO.File.WriteAllBytes(severFilePath + newFile, base64AcsEncodedBytes);
}
.
.
}

Angular js posting object to web API is null

I am posting form data to webAPI and one of object has boolean value(i.e from checkbox; Deviceselected has boolean values here in code).this object returns null in my api controller.
I tried declaring Desktop and Mobile as string in controller.That did not fix as well.
What am i missing in here?
I'm able to post other data except Deviceselected
Angualrjs controller code
$scope.SendData = function (Data) {
var GetAll = new Object();
GetAll.Redirection = Data.redirection;
GetAll.Deviceselected = new Object();
GetAll.Deviceselected.Desktop = Data.devSelected.desktop;
GetAll.Deviceselected.Mobile = Data.devSelected.mobile;
GetAll.Protocol = Data.protocol;
$http({
url: "http://localhost:61352/api/Market",
dataType: 'json',
method: 'POST',
data: GetAll,
headers: {
"Content-Type": "application/json"
}
}).then(successCallback, errorCallback);
};
})
Web API code
public class SubmitData
{
public string Redirection { get; set; }
public Deviceselected deviceSelected;
public string Protocol { get; set; }
}
public class Deviceselected
{
public Boolean Desktop { get; set; }
public Boolean Mobile { get; set; }
}
[HttpPost]
public string sendData(HttpRequestMessage request,[FromBody] SubmitData marketModel)
{
return "Data Reached";
}
Apparently it works with the same logic.1)Cleared cache 2)Run API and then load HTML

angularjs $http post object always null

I'm trying to send an object to the backend with an $http post, but one of the parameters is always null. I'm formatting the dto in the same way when saving a new object and that works fine, but when I try to call the update function it's not working. What am I missing?
This is my controller code:
vm.postUpdateITSM = function (itsm) {
$http({
method: "POST",
url: "api/sources/" + itsm.Id,
data: {
id: itsm.Id,
dto: {
ConnectorType: itsm.Type,
SourceName: itsm.ServerName,
DisplayName: itsm.DisplayName,
Credentials: JSON.stringify(itsm.UserName,
itsm.Password),
Url: itsm.URL,
Settings: JSON.stringify(itsm.ResolveAlerts ? itsm.ResolveAlerts : false,
itsm.AcknowledgeAlerts ? itsm.AcknowledgeAlerts : false,
itsm.SyncInterval,
itsm.IncidentInterval,
itsm.Status ? itsm.Status : "")
}
}
});
}
And on the back end: The dto is always null when called.
public async Task<IHttpActionResult> Update(int id, [FromBody] SourceDto dto)
{
var source = Mapper.Map<Source>(dto);
source.SourceID = id;
source.ServerCount = "";
var res = await SystemActors.SourceManager.Ask(new UpdateSource(source));
var failure = res as Status.Failure;
if (failure != null)
{
return InternalServerError();
}
var success = ((SqlResult<object>) res).Success;
if (!success)
{
return Content(HttpStatusCode.BadRequest, "Failed to update source.");
}
return Ok(new ResponsePackage {Success = true});
}
And this is the SourceDto class:
public class SourceDto
{
public string ConnectorType { get; set; }
public string SourceName { get; set; }
public string DisplayName { get; set; }
public string Credentials { get; set; }
public string Url { get; set; }
public string Settings { get; set; }
}
Your frontend data is formatted a bit wrong - the data parameter should just be the one object your ASP.NET controller is expecting in the [FromBody], your SourceDto model - and the id should be a query string:
method: "POST",
url: "api/sources/" + itsm.Id,
data: {
ConnectorType: itsm.Type,
SourceName: itsm.ServerName,
DisplayName: itsm.DisplayName,
Credentials: JSON.stringify(itsm.UserName,
itsm.Password),
Url: itsm.URL,
Settings: JSON.stringify(itsm.ResolveAlerts ? itsm.ResolveAlerts : false,
itsm.AcknowledgeAlerts ? itsm.AcknowledgeAlerts : false,
itsm.SyncInterval,
itsm.IncidentInterval,
itsm.Status ? itsm.Status : "")
}
});
ASP.NET will apply the request body to the expected model - if it doesn't match, you'll get null

HttpPostedFileBase is null - Posting files from AngularJS to MVC

Similar questions have been asked so many times, but there are no clear answers, I still have trouble getting mine to work.
This is the model in C#
public class SubmitModel
{
public string Name { get; set; }
public HttpPostedFileBase File { get; set; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
This is the MVC code
[HttpPost]
public ActionResult Test(SubmitModel model)
{
// Here model.File and model.Files is always null
}
This is what I submitted using AngularJS
var data = {
name: scope.name, // This is passed to MVC successfully
file: scope.files[0], // Doesn't even work with single file
files: scope.files // This is a FileList
};
$http.post("/umbraco/surface/MyController/Test", data).success(...);
If you want to know how I assign scope.files:
$('#upload').on('change', function (e) {
scope.$apply(function () {
scope.files = e.target.files;
});
});
Could someone see what I am missing?
Solved it!
This is how it should be submitted
var data = new FormData();
angular.forEach(scope.item, function (value, key) {
if (key == "files") {
for (var i = 0; i < value.length; i++) {
data.append(value[i].name, value[i]); // Filename:File
}
} else {
data.append(key, value);
}
});
$http.post("/umbraco/surface/MyController/Test", data, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).success(...);
Then in MVC, we get the files from Request.Files, it won't be in the model.
[HttpPost]
public ActionResult Test(SubmitModel model)
{
var files = Request.Files; // a collection of HttpPostedFileBase
Save(model, files);
}
More info:
https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
http://www.codeproject.com/Tips/878730/File-Upload-Using-AngularJS-and-ASP-NET-MVC

System.NullReferenceException in controller ASP.Net MVC

Working with (simple bowling game web app) where there are two text boxes and one submit button.
text box 1 = FirstRoll ,text box 2 = SecondRoll
User enter two numbers and cilck submit button then displays a score.
Issue : Getting System.NullReferenceException in Submit action method. Why frame is empty in first instance?
Controller
[HttpPost]
public JsonResult Submit(Frame[] _frames)
{
int result= 0;
var objBowlingScore = new GameEngineService();
foreach(var frame in _frames)
{
result = objBowlingScore.CalculateFrameScore(frame);
}
return Json(objBowlingScore);
}
Model
server side check validations are removed for code readability
public class Frame
{
public int FrameId { get; set; }
public int FirstRoll { get; set; }
public int SecondRoll { get; set; }
public int ThirdRoll { get; set; }
public int Score { get; set; }
}
View
<p>1st Roll: #Html.EditorFor(m => m.FirstRoll)</p>
<p>2nd Roll: #Html.EditorFor(m => m.SecondRoll)</p>
<button id="submitButton" class="btn btn-primary btn-lg">Submit Score</button>
<p><label>The current frame is : </label><label id="lblFrameCount"></label></p>
<p><label>The current score is : </label><label id="lblTotalScore"></label></p>
#section DocumentReady {
<script type="text/javascript">
var bowlingData = { "frames": [] };
$('#submitButton').click(function (e) {
var temp = {
"firstroll": $("#FirstRoll").val(),
"secondroll": $("#SecondRoll").val()
};
bowlingData.frames.push(temp);
var element = this;
$.ajax({
url: "/Home/Submit",
type: "POST",
data: JSON.stringify(bowlingData),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
$("#lblTotalScore").text(data.Score);
$("#FirstRoll").val("");
$("#SecondRoll").val("");
},
error: function () {
alert("An error has occured!!!");
}
});
});
</script>
Service
....
public int CalculateFrameScore(Frame _frame)
{
return _frame.FirstRoll + _frame.SecondRoll + _frame.ThirdRoll;
}
....
The issue was the parameter name in the Submit method didn't match with the parameter used in the jQuery event handler.
_frames should be frames. (i.e.
Was
public JsonResult Submit(Frame[] _frames))
Should be
public JsonResult Submit(Frame[] frames))

Resources