0

使用 MVC 3

我有一个保存 mvc questionare 表单的按钮。该表单将包含 50 个或更多问题以及用户答案。我决定对表单进行序列化,而不是将答案放在一个数组中发送给控制器。不确定如何阅读表格并获取问题 ID 和答案?

阿贾克斯调用:

$.ajax({
        url: '/Question/SaveQuestionaire',
        type: 'POST',
        cache: false,
        dataType: 'json',
        data: $("#SignupForm").serialize(), // creates an object for you

看法:

@using COPSGMIS;
@model IEnumerable<COPSGMIS.Models.Quiz>

@{
    ViewBag.Title = "Questionaire";
}
<h2>Questionaire</h2>
  <input type="hidden" id="currentstep" name="currentstep" />
   @using (Html.BeginForm("Questionaire", "Question", FormMethod.Post, new { id = "SignupForm" }))
   {
       <div id="wizardtemplate">       
            @foreach (var step in Model)       
            {
              <fieldset class="wizard">
                <div class="page_Title"> @Html.DisplayFor(modelItem => step.Title)</div>               
                @foreach (var question in step.Results)
                {
                ... code removed ....
                <label for="question">@Html.DisplayFor(modelItem => question.NumberedQuestion)</label>  
                 @Html.Raw(Html.DisplayControl(question.QuestionID, question.Choices, question.AnswerValue,question.ControlType)) 
                 </div>                               
                 @Html.Partial("_Comment", question)
               <hr />
                }
              </fieldset>
            }

呈现的 HTML:

<label for="question">3. This is a sample question (2) for the questionare?</label>  
                 <div class='answer'><input type='date' id='3' name='3' value='2012-12-10' /></div> 
                 </div>                               
 ... code removed ....

 <label for="question">4. This is a sample question (3) for the questionare?</label>  
                 <div class='answer'><input type='text' id='4' name='4' value='999' /></div> 
                 </div>           

模型:

 public class Quiz
    {
        public int ReviewID { get; set; }
        public int StepID { get; set; }
        public string Title { get; set; }
        public virtual IEnumerable<Result> Results { get; set; }
    }

我需要帮助

[HttpPost]
        public ActionResult SaveQuestionaire(int reviewid, Serialized? form)
.... code here

*更新的代码*请看这里

我的阿贾克斯电话:

function saveQuestioniare() {
           alert('Here');
           $.ajax({
            url: '/Question/SaveQuestionaire',
            type: 'POST',
            cache: false,
            dataType: 'json',
            data: {reviewid: 8, col:$("#SignupForm").serialize()},
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown);
                },
            success: function (json) {
                alert("hERE!");
            }
        });
    }

我的控制器:

 [HttpPost]
        public ActionResult SaveQuestionaire(int? id, FormCollection col)
        {
            using (var db = new NexGenContext())
            {
                foreach (var key in col.AllKeys.Where(q => !q.Contains("Comment_")))
                {
                    var answer = col[key];
                    int questionId = Convert.ToInt32(key);
                    db.Database.ExecuteSqlCommand(
                            "EXEC SP_AddUpdateResponse @QuestionID, @Reviewer, @AnswerValue, @ReviewID",
                            new SqlParameter("@QuestionID", questionId),
                            new SqlParameter("@Reviewer", "TEST - CG"),
                            new SqlParameter("@AnswerValue", answer),
                            new SqlParameter("@ReviewID", id)
                        );
                }
            }
            return Json("save", JsonRequestBehavior.AllowGet);
        }
4

1 回答 1

1

你有没有尝试过

$.ajax({
        url: '/Question/SaveQuestionaire',
        type: 'POST',
        cache: false,
        dataType: 'json',
        data: {model:$("#SignupForm").serialize()},

在服务器端

[HttpPost]
public ActionResult SaveQuestionaire(int? reviewid, IList<Quiz> model)

或获取已发布表单值的老式方法FormCollection

[HttpPost]
 public ActionResult SaveQuestionaire(int? reviewid, FormCollection col)
于 2013-01-04T20:36:20.503 回答