Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
what is happening is : I have two methods in controllers (ASP.NET MVC). One function is to update and the other is to insert Questionnaires. Now, here is the JavaScript and HTML code for taking and binding data to Stored Controller action method.


function updateQuestionnaire() {
       var questions = new Array();
       for (var i = 0; i < rowIds.length; i++) {
               debugger;
           var row = $(this);
           var question = {};
           debugger;
           var selectElement = document.querySelector('#answerType-' + rowIds[i]);
           var output = selectElement.options[selectElement.selectedIndex].value; //code restored
           question.SelectedAnswerType = output;
           question.AnswerType = output;
           question.CreModUser = @Session["UserId"];
           questions.push(question);
       }

       $.ajax({
           type: "POST",
           url: "/ChurnFB/UpdateQuestionnarie",
           data: JSON.stringify(questions),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (r) {
               alert(r + " record(s) inserted.");
           }
       });
   }


and then here is the HTML Part:


<
@foreach (var item in Model)
{
<td>
@Html.DropDownList("Answer", new SelectList(ViewBag.AnswerTypes, @item.SelectedAnswerType), "Select Any", new { id = "answerType-"+@item.RowId+"", style = "max-width:100px;" })



To make things simpler i have highlighted the problem variable in BOLD. The element AnswerType is a List Of char type.
When i am clicking on Update button in controller action method i am getting list which i am later converting to DataTable. Thing is when i am using the Insert Function:below for reference) AnswerType Count is not equal to zero. But in Update the same variable count is coming as (which later on fails in my stored procedure which i am using in action method.).



function saveAllQuestions() {
       var questions = new Array();
       var counter = 0;
       $("#tblQuestion TBODY TR").each(function () {
           var row = $(this);
           var question = {};
           debugger;

           var selectElement = document.querySelector('#ddlAnswerType' + counter);
           var output = selectElement.options[selectElement.selectedIndex].value;
           question.SelectedAnswerType = output;
           question.CreModUser = @Session["UserId"];
           questions.push(question);
           counter++;
       });

       //Send the JSON array to Controller using AJAX.
       $.ajax({
           type: "POST",
           url: "/ChurnFB/InsertQuestionnarie",
           data: JSON.stringify(questions),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (r) {
               alert(r + " record(s) inserted.");
           }
       });


Where am i going wrong? I am baffled as to how the same two variables in different situation are not binding..

What I have tried:

Googled samples but does not help.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900