Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# 尝试提交仅部分表单绑定到模型的表单值(使用ASP.Net MVC3)_C#_Asp.net Mvc_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

C# 尝试提交仅部分表单绑定到模型的表单值(使用ASP.Net MVC3)

C# 尝试提交仅部分表单绑定到模型的表单值(使用ASP.Net MVC3),c#,asp.net-mvc,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc,Asp.net Mvc 3,Entity Framework,我为下面的文字墙提前道歉。我希望一次提供所有可能需要的信息来理解我在做什么 我有一个收集一些基本用户信息(姓名、电子邮件等)的页面。在这一页的另一部分,我有一个选择题测验表格(每个问题使用无线电输入)。我的目标是收集联系信息,处理测验答案,并将带有联系信息的分数存储在QuizResults表中 这可能是一种过度思考或过度设计的情况。。所以请随意告诉我,我完全错了 以下是我的测验相关模型: public class QuizType { public QuizType() {

我为下面的文字墙提前道歉。我希望一次提供所有可能需要的信息来理解我在做什么

我有一个收集一些基本用户信息(姓名、电子邮件等)的页面。在这一页的另一部分,我有一个选择题测验表格(每个问题使用无线电输入)。我的目标是收集联系信息,处理测验答案,并将带有联系信息的分数存储在QuizResults表中

这可能是一种过度思考或过度设计的情况。。所以请随意告诉我,我完全错了

以下是我的测验相关模型:

public class QuizType {

    public QuizType() {
        this.QuizQuestions = new HashSet<QuizQuestion>();
        this.QuizResults = new HashSet<QuizResult>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<QuizQuestion> QuizQuestions { get; set; }
    public virtual ICollection<QuizResult> QuizResults { get; set; }
}

public class QuizQuestion {

    public QuizQuestion() {
        this.QuizAnswers = new HashSet<QuizAnswer>();
    }

    public int Id { get; set; }
    public string Question { get; set; }
    public int Order { get; set; }
    public int QuizTypeId { get; set; }

    public virtual ICollection<QuizAnswer> QuizAnswers { get; set; }
    public virtual QuizType QuizType { get; set; }
}

public class QuizResult {
    public int Id { get; set; }
    public string TesterName { get; set; }
    public string TesterEmail { get; set; }
    public string TesterCompany { get; set; }
    public string TesterPhone { get; set; }
    public string ApproverName { get; set; }
    public string ApproverEmail { get; set; }
    public bool HasCompanyIssuedIdBadge { get; set; }
    public int Score { get; set; }

    public virtual QuizType QuizType { get; set; }
}

public class QuizAnswer {

    public QuizAnswer() {
    }

    public int Id { get; set; }
    public bool isCorrectAnswer { get; set; }
    public string Answer { get; set; }
    public int QuizQuestionId { get; set; }

    public virtual QuizQuestion QuizQuestion { get; set; }
}
我不会发布我的整个razor视图,但我认为以下是相关部分:

@model QuizViewModel

@* *** MISC STYLE, JS, ETC LEFT OUT FOR BREVITY *** *@

@using (Html.BeginForm()) {

    @Html.LabelFor(model => model.QuizResults.TesterName)<br />
    @Html.EditorFor(model => model.QuizResults.TesterName)

    @* *** OTHER FORM FIELDS LEFT OUT FOR BREVITY *** *@

    foreach (var item in Model.QuizQuestions) {
        <div class="wizard-step">
            <h3>@item.Question</h3>
            @{
                // I've been tinkering with this trying to find a way to get it
                // so that the input would have the right id to be picked up and
                // dropped into my post object correctly
                var inputId = "SelectedAnsers[" + item.Id + "]";
            }
            @foreach (var answer in item.QuizAnswers) {
                <input type="radio" id="@inputId" name="@inputId" value="@answer.Id" /> @answer.Answer @:(@answer.isCorrectAnswer)
                <br />
            }
        </div>
    }

}
在我的头脑中,我希望能够处理测验表单的值,计算分数,然后根据填写的联系信息和新计算的分数创建一个新的QuizResult对象。如果我能找到正确的方法,那我就倒霉了


任何指针?

您可以添加另一个包含所有已发布值的类型参数

[HttpPost]
public ViewResult DukeDrive(QuizViewModel quizViewModel, FormCollection formData) {
    // I have a breakpoint set so that I can inspect quizViewModel
    return View();
}

哦,我没想到两个都能通过!我会很快给你一个机会。是的,这会让我到达我需要的地方。。。当然就这么简单。谢谢
@model QuizViewModel

@* *** MISC STYLE, JS, ETC LEFT OUT FOR BREVITY *** *@

@using (Html.BeginForm()) {

    @Html.LabelFor(model => model.QuizResults.TesterName)<br />
    @Html.EditorFor(model => model.QuizResults.TesterName)

    @* *** OTHER FORM FIELDS LEFT OUT FOR BREVITY *** *@

    foreach (var item in Model.QuizQuestions) {
        <div class="wizard-step">
            <h3>@item.Question</h3>
            @{
                // I've been tinkering with this trying to find a way to get it
                // so that the input would have the right id to be picked up and
                // dropped into my post object correctly
                var inputId = "SelectedAnsers[" + item.Id + "]";
            }
            @foreach (var answer in item.QuizAnswers) {
                <input type="radio" id="@inputId" name="@inputId" value="@answer.Id" /> @answer.Answer @:(@answer.isCorrectAnswer)
                <br />
            }
        </div>
    }

}
[HttpPost]
public ViewResult DukeDrive(QuizViewModel quizViewModel) {
    // I have a breakpoint set so that I can inspect quizViewModel
    return View();
}
[HttpPost]
public ViewResult DukeDrive(QuizViewModel quizViewModel, FormCollection formData) {
    // I have a breakpoint set so that I can inspect quizViewModel
    return View();
}