在将模型属性传递给ASP.Net MVC中的函数时获取空值

时间:2019-06-20 03:17:55

标签: javascript c# asp.net-mvc razor

我在javascript中有一个数组,用于存储所有字段值,并将该数组传递给模型属性AddChecklistSurveyResult。提交页面后,尝试在调用服务时将此模型属性作为参数传递给函数SaveLpgResult内的函数,它显示为Null,预先感谢。

模型(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));

            });

控制器

[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
}

我期望这样的结果:

[
  {
    "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
   }
 ]

1 个答案:

答案 0 :(得分:0)

最后,我解决了这个问题。在按钮内添加了一个onclick事件,用于调用javascript函数

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