2

Hi all I'm using ajax to call detail model by Id. But I want to display a message if model return data is null. How do I ?

my code ajax to display details model

$('#PGId').blur(function () {
        var errormsg = "";
        var id = $('#PGId').val();
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetDetailPG", "TimeSheetHeader")',
            data: { pgId: id },
            dataType: "json",
            success: function (data) {
               success: function (data) {
                if (data.isValid) {
                    $("#FullName").text(data.FisrtName + " " + data.LastName)
                    $('.Shiff[value="' + data.ShiffId + '"]').prop('checked', true)
                }
                else {
                    alert(data.error);
                }
            },
            },
            error: function () {

            }
        });
    })

my controller to bind data

[HttpGet]
    public ActionResult GetDetailPG(string pgId)
    {
        PGProfileViewModel pgProfileModel = new PGProfileViewModel();
        pgProfileModel.PGId = pgId;
        var query = _pgProfileService.GetPGProfileById(pgProfileModel.PGId);
        var model = query.ToViewModel();

        if (model == null)
        {
            return Json(new {isValid = false, error = "Error Message"}, JsonRequestBehavior.AllowGet);
        }
        else
        {
            model.LastName = query.LastName.Trim();
            model.FisrtName = query.FisrtName.Trim();
            model.ShiffId = query.ShiffId;
            return Json(new { model = model , isValid = true }, JsonRequestBehavior.AllowGet);
        }
    }
Khiem Nguyen
  • 403
  • 1
  • 8
  • 25
  • The same way you return when it's successful and with `HTTP 404` set additionally? – zerkms Jan 14 '15 at 04:43
  • What happened to your last question - just as I was about to post an answer you deleted it? - note the link you included did not fully solve your problem since it did not post back anything identifying the object! –  Jan 19 '15 at 04:08
  • sorry @StephenMuecke I have just deleted my question. I'll asked a new question to help you understand my question easier :) please wait me – Khiem Nguyen Jan 19 '15 at 04:26
  • @StephenMuecke I have just asked new question at here http://stackoverflow.com/questions/28019148/how-do-i-write-a-ajax-event-of-checkbox-to-pass-value-id-immediately-on-row-to-c – Khiem Nguyen Jan 19 '15 at 06:39

1 Answers1

2

If you returns a json object (as business model error) the javascript data value (which has the request result) will never be null.

You can try an approach like this:

C#

[HttpGet]
public ActionResult GetDetailPG(string pgId)
{
  PGProfileViewModel pgProfileModel = new PGProfileViewModel();
  pgProfileModel.PGId = pgId;
  var query = _pgProfileService.GetPGProfileById(pgProfileModel.PGId);
  var model = query.ToViewModel();

  if (model == null)
  {
    return Json({
      isValid: false,
      error: "Your error message"
    }, JsonRequestBehavior.AllowGet);
  }
  else
  {
    model.LastName = query.LastName.Trim();
    model.FisrtName = query.FisrtName.Trim();
    model.ShiffId = query.ShiffId;

    return Json({
      model: model,
      isValid: true
      }, JsonRequestBehavior.AllowGet);
   }
}

JS

//success function of your ajax request.
success: function (data) {
  if (data.isValid) {
    $("#FullName").text(data.FisrtName + " " + data.LastName)
    $('.Shiff[value="' + data.ShiffId + '"]').prop('checked', true)
  }
  else {
    alert(data.error);
  }
}
ianaya89
  • 4,153
  • 3
  • 26
  • 34
  • based on the original users comment to his own post he wants to send the error message from the controller. You should update the code to include an additional error message property in the json object that can be used in the js else statement. – wigs Jan 14 '15 at 05:06