Home » Jquery » javascript – Get the identifier name from ajax form data in asp.net mvc

javascript – Get the identifier name from ajax form data in asp.net mvc

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am using jquery ajax form data method to save some bulk attachments. Each attachment has a Guid as its formdata identifier which is the foreign key of another record from another table to which these attachments belong to.

Here is my client side code:

function saveAttachments(surveyAttachments, surveyId) {
            //surveyId is a Guid of a record from another table
            var data = new FormData();
            var _files = $(surveyAttachments).prop("files");
            for (i = 0; i < _files.length; i++) {
                //This next line has the SurveyId
                data.append(**surveyId**, _files[i]);
            }
            $.ajax({
                type: "POST",
                url: _URLSaveAttachments,
                dataType: "json",
                data: data,
                contentType: false,
                processData: false,
                success: function (response) {
                    /*response will be 1 if success*/
                    if (response == MethodStatus.Success) {
                        CJAlert(alertType.Success, "Campaign Saved Successfully<br/>Redirecting to Campaigns Table...");
                        setTimeout(function () {
                            window.location.replace(_URLCampaign);
                        }, 2000);
                    }
                    else {
                    /*response will be -1 if exception*/
                        CJAlert(alertType.Error, "Something went wrong with the server.");
                        hideLoader();
                    }
                },
                failure: function (response) {
                    //alert(response.responseText);
                },
                error: function (response) {
                    //alert(response.responseText);
                }
            });
        }

Now my problem is to know if there is a way to get that SurveyId from the 5th line of my client code.

Here is my JsonResult:

public JsonResult SaveSurveyAttachments()
        {
            MethodStatus Result = MethodStatus.None;
            try
            {
                if (Request.Files.Count > 0)
                {
                    AttachmentsModel objAttachmentsModel = new AttachmentsModel();
                    List<Attachments> AttachmentsList = new List<Attachments>();
                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                        Attachments objAttachments = new Attachments();
                        HttpPostedFileBase file = Request.Files[i];
                        string FileName = "~/" + Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                        file.SaveAs(Server.MapPath(FileName));
                        TotalSaved++;

                        objAttachments.Id = Guid.NewGuid();
                        objAttachments.TableName = "Survey";
                        //The next line needs the SurveyId
                        objAttachments.RowId = SurveyId;
                        objAttachments.AbsolutePath = FileName;
                        AttachmentsList.Add(objAttachments);
                    }
                    Result = (MethodStatus)objAttachmentsModel.AddOrUpdateAttachments(AttachmentsList);
                }
                else
                {
                    Result = MethodStatus.NullInputs;
                }
            }
            catch (Exception ex)
            {
                CommonModels objCommonModels = new CommonModels();
                objCommonModels.LogException(ex);
                Result = MethodStatus.Error;
            }
            return Json(
                Convert.ToInt32(Result),
                JsonRequestBehavior.AllowGet);
        }

Now my problem is to know if there is a way to get that SurveyId from the 20th line of my Json code.

How to&Answer: