Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MVC-Razor复杂嵌套表单提交不具有约束力_C#_Asp.net Mvc_Razor_Form Submit_Nested Forms - Fatal编程技术网

C#MVC-Razor复杂嵌套表单提交不具有约束力

C#MVC-Razor复杂嵌套表单提交不具有约束力,c#,asp.net-mvc,razor,form-submit,nested-forms,C#,Asp.net Mvc,Razor,Form Submit,Nested Forms,长期潜伏者。。。第一次提问。。。 我有一个复杂的表单,提交时返回null。本质上,我正在尝试构建一个数据库驱动的表单 该表单包含一个部分或问题列表 部分包含另一部分或问题的列表 Model 1: public FormViewModel { public List<FormSetsViewModel> formSets { get; set; } } Model 2: public FormSetsViewModel{ QAViewModel questionAn

长期潜伏者。。。第一次提问。。。 我有一个复杂的表单,提交时返回null。本质上,我正在尝试构建一个数据库驱动的表单

该表单包含一个部分或问题列表 部分包含另一部分或问题的列表

Model 1:
public FormViewModel {
    public List<FormSetsViewModel> formSets { get; set; }
}

Model 2: 
public FormSetsViewModel{
    QAViewModel questionAnswerViewModel { get; set; }
    SectionViewModel sectionViewModel { get; set; }
    bool isQuestion { get; set; }
    bool isSection { get; set; }
}

Model 3:
public SectionViewModel {
    public List<FormSectionQuestionsViewModel> formSectionQuestions { get; set; }
}

Model 4:
public FormSectionQuestionsViewModel {
    public QuestionAnswerViewModel questionAnswers;
    public SectionViewModel childSection;
    int orderNumber;
}

Model 5: 
public QAViewModel {
    int id { get; set; }
    string answer { get; set; }
    string question { get; set;}
}
Model 1:
公共窗体视图模型{
公共列表表单集{get;set;}
}
模式2:
公共表单集视图模型{
QAViewModel问题解答视图模型{get;set;}
SectionViewModel SectionViewModel{get;set;}
bool是问题{get;set;}
布尔isSection{get;set;}
}
模式3:
公共部分视图模型{
公共列表formSectionQuestions{get;set;}
}
模式4:
公共表单部分问题视图模型{
公共问答视图模型问答;
公共部分视图模型儿童部分;
int订单号;
}
模式5:
公共QAViewModel{
int id{get;set;}
字符串答案{get;set;}
字符串问题{get;set;}
}
意见如下:

FormViewModel.cshtml
@model FormViewModel
@using (Html.BeginForm("Save", "Forms"))
{
    <div class="row">
        @Html.EditorFor(model => model.formSetsViewModels)
    </div>

    <div class="controls">
        <input type="submit" value="Confirm" class="button" name="save" />
    </div>
}

@model FormSetsViewModel
<div class="control-group">
    @if (Model.isQuestion)
    {
        @Html.EditorFor(m => m.questionViewModel);
    }
    else
    {
        @Html.EditorFor(m => m.sectionViewModel);
    }
</div>

SectionViewModel.cshtml
@model SectionViewModel
@Html.EditorFor(m => m.formSectionQuestions)

FormSectionQuestionsViewModel.cshtml
@model FormSectionQuestionsViewModel
@if (Model.childSection != null)
{
    @Html.EditorFor(m => m.childSection)
}
else
{
    @Html.EditorFor(m => m.questionAnswers)
}

QAViewModel.cshtml
@model QAViewModel

<p><div class="question-text-edit">@Html.Raw(Model.questionText)</div>
@Html.TextAreaFor(m => m.answer, new { style = "width: 90%; height: 80px;" })
FormViewModel.cshtml
@模型FormViewModel
@使用(Html.BeginForm(“保存”、“表单”))
{
@EditorFor(model=>model.formSetsViewModels)
}
@模型FormSetViewModel
@如果(Model.isQuestion)
{
@EditorFor(m=>m.questionViewModel);
}
其他的
{
@EditorFor(m=>m.sectionViewModel);
}
SectionViewModel.cshtml
@模型截面视图模型
@EditorFor(m=>m.formSectionQuestions)
FormSectionQuestionsViewModel.cshtml
@模型表单部分问题视图模型
@if(Model.childSection!=null)
{
@EditorFor(m=>m.childSection)
}
其他的
{
@EditorFor(m=>m.questionAnswers)
}
QAViewModel.cshtml
@模型QAViewModel
@Html.Raw(Model.questionText)
@TextAreaFor(m=>m.answer,新的{style=“宽度:90%;高度:80px;”})
控制员:

[HttpPost]
public ActionResult Save(int caseID, List<FormSetsViewModel> formSets = null)
{
    return Index(caseID);
}
[HttpPost]
公共操作结果保存(int caseID,List formset=null)
{
返回指数(caseID);
}
视图作为数据库驱动的表单非常有效。但是,当我提交表单时,似乎表单集无法绑定,并返回null

从Html中,它创建了如下输入:

<input id="formSetsViewModels_d762713a-7a2f-497a-9417-4c6e91d33cb8__sectionViewModel_formSectionQuestions_48e738da-10d3-4518-be59-2493e2b7a7cc__questionAnswers_answer" name="formSetsViewModels[d762713a-7a2f-497a-9417-4c6e91d33cb8].sectionViewModel.formSectionQuestions[48e738da-10d3-4518-be59-2493e2b7a7cc].questionAnswers.answer" type="text" value="">

终于找到了答案

中的
FormSetViewModel
的变量名

public ActionResult Save(int caseID, List<FormSetsViewModel> formSets = null)
公共操作结果保存(int caseID,List formset=null) 需要
FormSetViewModel
才能绑定模型

另一件事是,类中的一些公共变量没有
{get;set;}
方法。
我们想要绑定的所有变量都需要
{get;set;}
方法。添加此选项可以解决此问题。

我认为您的问题与在对象列表上使用EditorFor有关。在这里,我可以看出EditorFor列表对象适用于更简单的版本。虽然在这种情况下我不确定。