简体   繁体   English

尝试提交表单值,其中只有部分表单绑定到模型(使用ASP.Net MVC3)

[英]Trying to submit form values where only part of the form binds to a model (with ASP.Net MVC3)

I apologize in advance for a wall of text below. 我提前为下面的一面墙文道歉。 I hope to provide all the info at once that may be needed to understand what I'm doing. 我希望能够提供所有可能需要的信息来了解我正在做的事情。

I've got a page where I'm collecting some basic user info (name, email, etc). 我有一个页面,我正在收集一些基本的用户信息(姓名,电子邮件等)。 In another portion of the page I have a multiple choice quiz form (using radio inputs for each question). 在页面的另一部分,我有一个多项选择测验表格(每个问题使用无线电输入)。 My goal is to collect the contact info, process the quiz answers, and store a score with the contact info in a QuizResults table. 我的目标是收集联系信息,处理测验答案,并在QuizResults表中存储包含联系信息的分数。

This may be a case of over thinking, or over engineering.. so feel free to tell me I'm going about this all wrong. 这可能是一个过度思考或过度工程的情况......所以请随意告诉我,我这样做是错的。

Here's my Quiz related models: 这是我的测验相关模型:

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

So basically I can create a Quiz Type, then for each type I can create multiple questions, each question can have multiple answers. 所以基本上我可以创建一个测验类型,然后对于每种类型我可以创建多个问题,每个问题可以有多个答案。 I'm not trying to post back any data for QuizType, QuizQuestion, or QuizAnswer. 我不是要回复QuizType,QuizQuestion或QuizAnswer的任何数据。 Those are just there to help build my page[s]. 那些只是帮助建立我的页面[s]。

This is where I start scratching my head. 这是我开始摸不着头脑的地方。 I need to be able to iterate through QuizQuestions and QuizAnswers to create the multiple choice form. 我需要能够遍历QuizQuestions和QuizAnswers以创建多选形式。 But I also need to bind part of that form to QuizResults for posting back.... here's what I've got now in order to display the form (but not work correctly). 但是我还需要将该表单的一部分绑定到QuizResults以便回发....这是我现在为了显示表单而已经得到的(但是没有正常工作)。

First, I've created a ViewModel: 首先,我创建了一个ViewModel:

public class QuizViewModel {
    public IQueryable<QuizQuestion> QuizQuestions { get; set; }
    public QuizResult QuizResults { get; set; }
}

Then in my controller: 然后在我的控制器中:

    public ViewResult Index() {

        var questions = 
            from q in unitOfWork.QuizQuestionRepository.Get() 
            where q.QuizType.Name.Equals("QuizTypeOne")
            select q;

        var qvm = new QuizViewModel { 
            QuizQuestions = questions
        };

        return View(qvm);
    }

I won't post my entire razor view, but I think these are the pertinent parts: 我不会发布我的整个剃刀视图,但我认为这些是相关的部分:

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

}

Back in my controller I have a method setup to handle the post, but it's not doing anything at the moment. 回到我的控制器,我有一个方法设置来处理帖子,但它目前没有做任何事情。 I'm just running in debug and looking at the values posted when I submit the form. 我只是在调试中运行并查看提交表单时发布的值。

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

In my head I want to be able to process the quiz form values, calculate the score, then create a new QuizResult object based on the contact info filled out and the newly calc'd score. 在我的脑海中,我希望能够处理测验表格值,计算得分,然后根据填写的联系信息和新计算得分创建一个新的QuizResult对象。 I'll be darned if I can find the right way to do it though. 如果我能找到正确的方法,我会很高兴。

Any pointers? 有什么指针吗?

You could add another parameter of type FormCollection which contains all posted values. 您可以添加另一个FormCollection类型的参数,其中包含所有已发布的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM