1

I have an array inside javascript for storing all field values and am passing that array to a model property AddChecklistSurveyResult. After submitting the page am trying to pass this model property as a parameter inside a function SaveLpgResult while calling service, It's showing Null, Thanks in advance.

MODEL (QAGInspectionViewModel )

public string AddChecklistSurveyResult { get; set; }

HTML

    @using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

 <table>
        <tr>
            <td colspan="2">
                <button type="submit" class="btn btn-success">Save</button>
            </td>
        </tr>
    </table>
 @Html.HiddenFor(model => model.AddChecklistSurveyResult);
}

JAVASCRIPT

 function LpgChecklist() 
{
  $.each(answers, function (index, value) {
                surveyIdList = surveyIdObj
                surveyTemplateList = surveyTemplateObj
                answerList = value.answerListId;
                question = value.questionId;
                value = value.value;
                ChecklistDetails.push({
                    answerArr: answerList,
                    QuestionArr: question,
                    valueArr: value
                });

                console.log(ChecklistDetails);

                $("#AddChecklistSurveyResult").val(JSON.stringify(ChecklistDetails));

            });

CONTROLLER

[HttpPost]
public ActionResult _QAGInspectionDetails(QAGInspectionViewModel model)
{
    var save = qAGCaseService.Create(model);
}

qAGCaseService (SERVICE)

public bool Create(QAGInspectionViewModel model)
{
    SaveLpgResult(model.AddChecklistSurveyResult, model.EntryBy);
    //Here Am getting Null for model.AddChecklistSurveyResult
}

Am Expecting result like this:

[
  {
    "LPG_Brand":"Caltex",
    "LPG_Status":null,
    "LPG_Capacity":"13.3",
    "LPG_Used":"",
    "LPG_Unused":"",
    "LPG_Quantity":"",
    "LPG_Detained":"Yes",
    "LPG_StorageLocation":"EMSD",
    "LPG_Brand_Display":null,
    "LPG_Capacity_Display":null,
    "LPG_Detained_Display":null,
    "LPG_StorageLocation_Display":null
   }
 ]
Rina
  • 41
  • 6
  • When I did Console.log( $("#AddChecklistSurveyResult").val(JSON.stringify(ChecklistDetails))) I got result as _jQuery.fn.init [input#AddLPGCylinderDetails]_ – Rina Jun 20 '19 at 04:08
  • Firstly, since you are using `$each` can you take `$("#AddChecklistSurveyResult").val(JSON.stringify(ChecklistDetails));` out of the `$each` block?, then before the line where you will have it, do a `Console.log(ChecklistDetails)`, also show how you are submitting the page – Bosco Jun 20 '19 at 06:04
  • I did Console.log(ChecklistDetails), Am getting data like this: _[{…}] 0: {answerArr: "1", QuestionArr: "3", valueArr: "OVEN"}_ – Rina Jun 20 '19 at 06:18
  • I had given – Rina Jun 20 '19 at 06:20
  • can you post html code from
    tag or from @html.beginform
    – Satish Patil Jun 20 '19 at 07:33
  • plus can you post generated hidden field from source of page – Satish Patil Jun 20 '19 at 08:04
  • Take a look at those answers: https://stackoverflow.com/questions/30757343/mvc-hidden-field-via-html-helper-in-form-post-issue or https://stackoverflow.com/questions/20657706/mvc-4-html-hiddenfor-are-not-updating-on-a-postback – Kenan Begić Jun 20 '19 at 08:20
  • I had found the problem when I submit the form the hiddenField value becomes null. when I put to check the hidden field value inside a javascript function, I am getting value. Gone through the document mentioned above. – Rina Jun 20 '19 at 08:53

1 Answers1

0

Finally, I solve the problem. Added an onclick event inside the button for calling the javascript function

 <button type="submit" class="btn btn-success" onclick="LpgChecklist()">Save</button>
Rina
  • 41
  • 6