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
   }
 ]
8
  • When I did Console.log( $("#AddChecklistSurveyResult").val(JSON.stringify(ChecklistDetails))) I got result as jQuery.fn.init [input#AddLPGCylinderDetails] Commented Jun 20, 2019 at 4: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 Commented Jun 20, 2019 at 6:04
  • I did Console.log(ChecklistDetails), Am getting data like this: [{…}] 0: {answerArr: "1", QuestionArr: "3", valueArr: "OVEN"} Commented Jun 20, 2019 at 6:18
  • I had given <button type="submit"> for form submittion Commented Jun 20, 2019 at 6:20
  • can you post html code from <form></form> tag or from @html.beginform Commented Jun 20, 2019 at 7:33

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.