如何读取ajax调用mvc中控制器发送的序列化表单的内容?

时间:2013-01-04 19:39:29

标签: asp.net-mvc-3 asp.net-ajax

使用MVC 3

我有一个保存mvc问题表单的按钮。表单将包含50个或更多用户答案的​​问题。我决定对表单进行搜索,而不是将答案放在一个数组中发送给控制器。不知道如何阅读表格并抓住问题ID和答案?

Ajax Call:

$.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 RENDERED:

<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

* 更新后的代码 *请在这里停下来

我的Ajax CALL:

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!");
            }
        });
    }

MY Controller:

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

1 个答案:

答案 0 :(得分: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)