ViewModel在提交时将空值传递给控制器

时间:2018-11-30 14:40:26

标签: c# asp.net-mvc

我的观点:

@using (Html.BeginForm("", "", FormMethod.Post, new { id = "responseEntryForm" }))
{

    <div id="SiteDeployment" class="tabcontent">
        @Html.HiddenFor(m=>m.Sections,Model.Sections)
        @for (int index = 0; index < Model.Sections.Count; index++)
        {
            <fieldset class="leftFieldset">
                <div class="inputFieldDiv">
                    <label class="GroupHeadings"> @Model.Sections[index].Name</label><br />

                    @for (int subIndex = 0; subIndex < Model.Sections[index].SubSections.Count; subIndex++)
                    {
                        <div style="width:100%">
                            <div class="SubGroups">
                                @Model.Sections[index].SubSections[subIndex].Name
                            </div>
                            <div class="subEntries">
                                @for (int subsubIndex = 0; subsubIndex < Model.Sections[index].SubSections[subIndex].QuestionsList.Count; subsubIndex++)
                                {
                                    <div class="subSections">
                                        <label class="StrucQuestions"> @Model.Sections[index].SubSections[subIndex].QuestionsList[subsubIndex].Question</label>
                                        @Html.DropDownListFor(m => m.Sections[index].SubSections[subIndex].QuestionsList[subsubIndex].Response, new SelectList(Model.ResponseList, "Value", "Text"), new { @class = "strucType", @id = "ddl_" + subIndex + Model.Sections[index].SubSections[subIndex].QuestionsList[subsubIndex].QuestionID })

                                    </div>
                                }
                            </div>
                        </div>
                    }
                </div>
            </fieldset>
        }

调用我的控制器并使用ajax调用按如下方式传递表单数据:

function submitResponses() {

   $.ajax({
       url: '@Url.Action("SaveResponsesData", "Dashboard")',
       datatype: 'json',
        type: "POST",
        data: $('#responseEntryForm').serialize(),
        success: function (result) {
            if (result == "T ") {
                alert("Save is successful");
            }


        }


    });

}

我的控制器如下:

[HttpPost]
    public ActionResult SaveResponsesData(ResponseEntryViewModel objResponseEntryViewModel)
    {
        // ViewBag.SelectedType = TypeValue.ToUpper();

            return View();


    }

我的ViewModel如下所示:

public class ResponseEntryViewModel
{
    public int TypeID { get; set; }
    public string TypeName { get; set; }
    public string CompletedBy { get; set; }
    public string CompletedOn { get; set; }
    public int User_ID { get; set; }
    public List<SectionDataModel> Sections = new List<SectionDataModel>();
    public IEnumerable<SelectListItem> ResponseList { get; set; }


    public class SectionDataModel
    {
        public int SectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int TypeId { get; set; }
         public List<SubSectionModel> SubSections = new List<SubSectionModel>();

    }

    public class SubSectionModel
    {
        public int SubSectionID { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public int SectionId { get; set; }
        public List<QuestionModel> QuestionsList = new List<QuestionModel>();

    }

    public class QuestionModel
    {
        public int SubSectionID { get; set; }
        public int QuestionID { get; set; }
        public string Question { get; set; }
        public bool Response { get; set; }

    }
}

在我的视图模型ResponseEntryViewModel中,我有一个部分列表,其中包含一个子部分列表,这些子部分进一步包含一个问题列表,并且用户正在从我的视图中输入对这些问题的响应。

当我单击提交时。我的viewModel没有任何值,Sections计数为0。

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

我不确定这是否是问题所在,但可能是这样。

datatype

应该是

dataType

答案 1 :(得分:0)

最简单的方法就是完全删除dataType属性。您正在发送表单编码的数据,而不是JSON。

如果要将JSON发送到服务器,则必须使用[FromBody]属性:

[HttpPost]
public ActionResult SaveResponsesData([FromBody] ResponseEntryViewModel objResponseEntryViewModel)
{
    // ...
    return View();
}

但是,您没有JSON格式的数据。对于JSON,通常使用JavaScript对象并在其上调用JSON.stringify(data)函数。 JQuery的{​​{1}}使用不同的格式进行表单发布-将一组表单元素编码为提交字符串。

这种格式的示例为:serialize()

您可能还需要设置single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1

答案 2 :(得分:0)

我认为,如果您创建一个单独的模型来保存响应,那会容易得多,例如,希望您能理解我要显示的内容:

public class SaveResponseModel
    {
        public int SubSectionID { get; set; }
        public int QuestionID { get; set; }
        public bool Response { get; set; }
    }


[HttpPost]
    public ActionResult SaveResponsesData(List<SaveResponseModel> responses)
    {
        // ViewBag.SelectedType = TypeValue.ToUpper();
        return View();
    }

.cshtml:

for each Sections, SubSection, Question...{
            <input name="responses.Index" type="hidden" value="@item.QuestionID" />
            <input name="responses[@item.QuestionID].QuestionID" type="hidden" value="@item.QuestionID" />
            <input name="responses[@item.QuestionID].SubSectionID" type="hidden" value="@item.SubSectionID" />
            @Html.DropDownList((string)string.Format("responses[{0}].Response", @item.QuestionID), new SelectList(Model.ResponseList, "Value", "Text"), new { @class = "strucType", @id = "ddl_" + subIndex + Model.Sections[index].SubSections[subIndex].QuestionsList[subsubIndex].QuestionID })

}