title: How to return json object on post request with message to ajax function
body: <p>I want to be able to return json object with a custom error/success message using the same line of code on post request: i have these two lines of code:</p>
  
  <pre><code>return Json(data);
  return Json(new { f = "error" });
  </code></pre>
  
  <p>I want to be able display it in one line like this:</p>
  
  <pre><code>return Json(data, Json(new { f = "error" }));
  </code></pre>
  
  <p>I know i can't have multiple return statements in my code. but i want to return the data with message.</p>
  
  <p>My ServerSide Code:</p>
  
  <pre><code>if (getId &gt; 0)
  {
      var getList = appointmentRepository.GetAppointmentList(userId);
      var data = Newtonsoft.Json.JsonConvert.SerializeObject(getList);                       
      return Json(data);
      return Json(new { s = "success" });
  }
  else
  {
      var getList = appointmentRepository.GetAppointmentList(userId);
      var data = Newtonsoft.Json.JsonConvert.SerializeObject(getList);
      return Json(data);
      return Json(new { f = "error" });
  }
  </code></pre>
  
  <p>My Ajax Fucntion:</p>
  
  <pre><code>&lt;script type = "text/javascript"&gt;
     $(document).ready(function () {
        $('#tblAppointment').DataTable({
           dom: 'Bfrtip',
           buttons: [
              'copyHtml5',
              'excelHtml5',
              'csvHtml5',
              'pdfHtml5'
           ]
        });
  
        var table = $("#tblAppointment").DataTable();
        $("#saveButton").click(function () {
  
           console.log("appDate:" + $('.datetimepicker').val());
           $.ajax({
              url: '/Appointment/InsertPatientAppointment/',
              type: "POST",
              data: JSON.stringify({
                 appointmentDate: $(".datetimepicker").val(),
                 patientRegNo: $("#patientRegNo").val(),
                 reasons: $("#reasons").val()
              }),
  
              cache: false,
              dataType: "json",
              contentType: "application/json; charset=utf-8",
              success: function (_data) {
                 if (_data.f !== undefined) {
                    swal({
                       title: "Failed!",
                       text: _data.f, //"Ooops! something went wrong, 
                       record not saved,
                       try again later ",
                       type: "info"
                    });
                    table.clear().draw();
                    //table.destroy();
                    // $("#viewReportBtn").prop("disabled", false);
                    return false;
                 } else {
                    swal({
                       title: "Success!",
                       text: _data.s, //"Appointment added successfully!",
                       type: "success"
                    });
  
                 }
                 $(".datetimepicker").val('');
                 $("#patientRegNo").val('');
                 $("#reasons").val('');
  
                 var arr = $.map(JSON.parse(_data), function (el) {
                    return
                    el
                 });
  
                 if (arr.length === 0) {
                    swal({
                       title: "No Record Found!",
                       text: _data.f, //"Your search returns an empty 
                       result set !",
                       type: "info"
                    });
                    table.clear().draw();
                    return false;
                 }
                 table.clear();
                 table.destroy();
                 $('#tblAppointment').dataTable({
                    data: arr,
                    columns: [{
                          "data": "MatricRegNo"
                       },
                       {
                          "data": "PatientName"
                       },
                       {
                          "data": "EntryDate"
                       },
                       {
                          "data": "AppointmentDate"
                       },
                       {
                          "data": "Reasons"
                       },
                       {
                          "data": function (data, type, row, meta) {
                             return '&lt;span class="fa fa-edit" data- 
                             toggle = "modal"
                             data - target = "#modal-Edit" &gt; &lt; /span&gt;';
                          }
                       }
                    ],
                    dom: 'Bfrtip',
                    buttons: [
                       'copy', 'csv', 'excel',
                       {
                          extend: 'pdfHtml5',
                          orientation: 'Portriat',
                          pageSize: 'A4'
                       }
                    ]
                 });
                 table = $("#tblAppointment").DataTable();
              }
           });
        });
     });
  &lt;/script&gt;
  </code></pre>