locked
Uncaught Reference Error:Data is not Defined RRS feed

  • Question

  • User210895818 posted

    I am trying to pass the model values to the controller through ajax, but getting an error of uncaught reference error and POST: 500 Internal Server Error. I think the values are not passing correctly but unable to track exact issue.

    Here is Model

            public string ProgramName { get; set; }
            public int ID { get; set; }
            public int Std_ref_id { get; set; }
            public int Program_ref_id { get; set; }
            public string Subject { get; set; }
            public string YOP { get; set; }
            public Nullable<decimal> Mark { get; set; }
    

    Controller Code:

    public ActionResult ConfirmEditStdQualif(StdAcademicQualifModel model)
           {
               Service_Std service_ = new Service_Std();
               TempData["Page"] = 3;
               TempData["StdId"] = model.Std_ref_id;
               if(ModelState.IsValid &&  model.Std_ref_id > 0)
               {
                   service_.UpdateStdAcademicQualif(model);
               }
               return RedirectToAction("Student", "Student");
           }

    View Code:

    @model GCWUS.Models.StdAcademicQualifModel
     @foreach (var item in Model.StdAcademicQualifList)
                        {
    <td>
    <a href="#" class="btn btn-sm btn-primary no-radius actEdit">
    <i class="ace-icon fa fa-pencil-square-o bigger-230 "></i>
    </a>
    <a href="#" class="btn btn-sm btn-danger no-radius actDelete">
    <i class="ace-icon fa fa-trash-o bigger-230"></i>
    </a>
    <a href="#" class="btn btn-sm btn-success no-radius actSave" style="display: none;">
    <i class="ace-icon fa fa-save bigger-230 "></i>
    </a>
    <a href="#" class="btn btn-sm btn-grey no-radius actCancel" style="display: none;">
    <i class="ace-icon fa fa-close bigger-230"></i>
    </a>
    </td> <td> <div> <input type="text" name="ProgramName" class="form-control actProgramName actUpdate" disabled value="@item.ProgramName" data-prev="@item.ProgramName" /> </div> </td> <td> <div> <input type="text" name="Subject" class="form-control actSubject actUpdate" disabled value="@item.Subject" data-prev="@item.Subject" /> </div> </td> <td> <div> <input type="text" name="YOP" class="form-control actYOP actUpdate" disabled value="@item.YOP" data-prev="@item.YOP" /> </div> </td> <td> <div> <input type="text" name="Marks" class="form-control actMarks actUpdate" disabled value="@item.Mark" data-prev="@item.lMark" /> </div> </td> <input type="hidden" value="@item.Std_ref_id" id="StdId" /> <input type="hidden" value="@item.Program_ref_id" id="Program_ref_id" /> <input type="hidden" value="@item.ID" id="Id" /> </tr> }

    and Ajax Code

    <script>       
    $(".actSave").on("click", function () { var $save = $(this); $tr = $save.closest('tr'); var StdId = $('#StdId').val(); var Id = $('#Id').val(); var Program_ref_id = $('#Program_ref_id').val(); Subject = $tr.find('.actSubject'); YOP = $tr.find('.actYOP'); Marks = $tr.find('.actMarks'); debugger; var StdAcademicQualifModel = { "Std_ref_id": StdId, "ID": Id, "Program_ref_id": Program_ref_id, "Subject": Subject, "UniversityBoard": Board, "YOP": YOP, "Marks": Marks, }; $.ajax({ url: '@Url.Action("ConfirmEditStdQualif", "Student")', contentType: 'application/json; charset=utf-8', data: JSON.stringify(StdAcademicQualifModel), type: 'POST', async: false, success: function (data) { Subject.attr('disabled', 'disabled'); YOP.attr('disabled', 'disabled'); Marks.attr('disabled', 'disabled'); var $edit = $tr.find('.actEdit'); var $save = $tr.find('.actSave'); var $cancel = $tr.find('.actCancel'); var $delete = $tr.find('.actDelete'); $edit.show(); $cancel.hide(); $save.hide(); $delete.show(); }, error: function () { //alert("No Record Found"); console.log("Error", data); } });
    </script>

    Saturday, December 8, 2018 3:32 PM

All replies

  • User1120430333 posted

    You probably need to put a VS debugger breakpoint in the C# controller and find out what is happening, use Quickwatch to look at model and single-step code . 

    Saturday, December 8, 2018 4:10 PM
  • User210895818 posted

    put a VS debugger breakpoint in the C# controller

    I already have a breakpoint at controller but it does not hit

    Saturday, December 8, 2018 4:17 PM
  • User1120430333 posted

    DA924

    put a VS debugger breakpoint in the C# controller

    I already have a breakpoint at controller but it does not hit

    If you are using IIS, then you have to know who  does what and how to do it.

    https://stackify.com/w3wp-exe-iis-worker-process/

    http://www.sharepointpals.com/post/How-to-Identify-and-Attach-Correct-W3WP-Process-in-Visual-Studio-while-debugging

    Saturday, December 8, 2018 5:07 PM
  • User1520731567 posted

    Hi Ridzi,

    According to your code,i find there are something worry in your ajax,

     var StdAcademicQualifModel = {
                     "Std_ref_id": StdId,
                     "ID": Id,
                     "Program_ref_id": Program_ref_id,
                     "Subject": Subject,
                     "UniversityBoard": Board,
                     "YOP": YOP,
                     "Marks": Marks,               
                 };

    data: JSON.stringify(StdAcademicQualifModel),

    You need to pay attention that fields in StdAcademicQualifModel = {}  can be missing, but must exist in StdAcademicQualifModel model.

    You just delete the field which is not stored in StdAcademicQualifModel :

    var StdAcademicQualifModel = {
                     "Std_ref_id": StdId,
                     "ID": Id,
                     "Program_ref_id": Program_ref_id,
                     "Subject": Subject,
                     "UniversityBoard": Board,
                     "YOP": YOP,
                     "Marks": Marks,               
                 };

    How my demo works:

    Best Regards.

    Yuki Tao

    Monday, December 10, 2018 7:54 AM
  • User210895818 posted

    Hi Yuki Tao,

    Thanks for your reply, In my model every field has a value. It's just a field of "overseas" in my model that is allowed to be empty and I already did not mention it in here:

    var StdAcademicQualifModel = {} 

    Monday, December 10, 2018 9:38 AM
  • User1356206773 posted

    Hi Ridzi,

    Try with below ajax code, It should work for you-

    $.ajax({
    url: '@Url.Action("ConfirmEditStdQualif", "Student")',
    contentType: 'application/json',
    data: StdAcademicQualifModel,
    type: 'POST',
    success: function (data) {

    Subject.attr('disabled', 'disabled');
    YOP.attr('disabled', 'disabled');
    Marks.attr('disabled', 'disabled');

    var $edit = $tr.find('.actEdit');
    var $save = $tr.find('.actSave');
    var $cancel = $tr.find('.actCancel');
    var $delete = $tr.find('.actDelete');

    $edit.show();
    $cancel.hide();
    $save.hide();
    $delete.show();
    },
    error: function () {
    //alert("No Record Found");
    console.log("Error", data);
    }
    });

    Monday, December 10, 2018 10:18 AM
  • User1520731567 posted

    Hi Ridzi,

    According to your httpost action:

    public ActionResult ConfirmEditStdQualif(StdAcademicQualifModel model)
           {
               //Service_Std service_ = new Service_Std();
               //TempData["Page"] = 3;
               //TempData["StdId"] = model.Std_ref_id;
               //if(ModelState.IsValid &&  model.Std_ref_id > 0)
               //{
               //    service_.UpdateStdAcademicQualif(model);
               //}
               return RedirectToAction("Student", "Student");
           }

    This action will jump to Student action finally, regardless of the return of this Student action,The compiler will eventually jump to error function.

    Generally,if there are success or error function in your ajax,I suggest you could return Json(); in ConfirmEditStdQualif httppost action,

    For example:

    public ActionResult ConfirmEditStdQualif(StdAcademicQualifModel model)
           {
               //Service_Std service_ = new Service_Std();
               //TempData["Page"] = 3;
               //TempData["StdId"] = model.Std_ref_id;
               //if(ModelState.IsValid &&  model.Std_ref_id > 0)
               //{
               //    service_.UpdateStdAcademicQualif(model);
               //}
               //return RedirectToAction("Student", "Student");
    return Json(new { aa= "111", bb= "222"}); }

    And then, in callback function in ajax:

    ...
    success: function (data) {
        alert(data.aa);
       alert(data.bb);
    
    }
    ...

    If you want to call Student acation, I suggest you could convert action to a void function, an then call it in httppost action.

    Then the compiler will come back and return Json,finally jump to success function in ajax.

    Best Regards.

    Yuki Tao

    Thursday, December 13, 2018 2:46 AM