如果数据库中没有记录,我应该从Controller返回什么,我想向用户说明这个事实?

时间:2014-02-13 10:27:25

标签: jquery asp.net-mvc

如果数据库中存在成员标识,我想自动填充文本框。否则我想取消“请输入正确的会员ID”

这是我的Jquery代码:

$('#memberid').focusout(function () {             
     $.getJSON('/ContactUs/GetMember/' + $('#memberid').val(), function (data) {
         $('#FName').val(data[0].M_Fname).attr("readonly", "readonly");
         $('#LName').val(data[0].M_Lname).attr("readonly", "readonly");
         $('#Email_Id').val(data[0].M_Emailid).attr("readonly", "readonly");
         $('#Cno').val(data[0].M_ContactNo).attr("readonly", "readonly");
         //alert(data);
         //alert(data[0].M_Fname);
     });
})

这是我的控制器:

public JsonResult GetMember(string id)
{
   int id1 =Convert.ToInt32(id);
   clubDataContext db = new clubDataContext();
   var result = from r in db.M_Registarions
                where r.M_id == (id1)
                select new { r.M_Fname, r.M_Lname, r.M_Emailid, r.M_ContactNo };
   if (result.Any())
       return Json(result, JsonRequestBehavior.AllowGet);
   else
       return Json(result, JsonRequestBehavior.AllowGet);
}

这里我返回JsonResult并使用getJSON从数据库中获取数据。

2 个答案:

答案 0 :(得分:2)

在我看来,控制器动作正是应该做的......如果没有找到任何内容,则不返回任何内容。只需检查客户端的空响应..

$('#memberid').focusout(function () {             
     $.getJSON('/ContactUs/GetMember/' + $('#memberid').val(), function (data) {
         if(data.length > 0){
             $('#FName').val(data[0].M_Fname).attr("readonly", "readonly");
             $('#LName').val(data[0].M_Lname).attr("readonly", "readonly");
             $('#Email_Id').val(data[0].M_Emailid).attr("readonly", "readonly");
             $('#Cno').val(data[0].M_ContactNo).attr("readonly", "readonly");
        }else{
            //NOTHING WAS FOUND
        }
     });
})

或者你可以返回一个Http 404(Not Found)并处理错误......

控制器:

public ActionResult GetMember(string id)
{
   int id1 =Convert.ToInt32(id);
   clubDataContext db = new clubDataContext();
   var result = from r in db.M_Registarions
                where r.M_id == (id1)
                select new { r.M_Fname, r.M_Lname, r.M_Emailid, r.M_ContactNo };
   if (result.Any())
       return Json(result, JsonRequestBehavior.AllowGet);
   else
       return HttpNotFound();
}

JavaScript的:

$('#memberid').focusout(function () {             
     $.getJSON('/ContactUs/GetMember/' + $('#memberid').val(), function (data) {
             $('#FName').val(data[0].M_Fname).attr("readonly", "readonly");
             $('#LName').val(data[0].M_Lname).attr("readonly", "readonly");
             $('#Email_Id').val(data[0].M_Emailid).attr("readonly", "readonly");
             $('#Cno').val(data[0].M_ContactNo).attr("readonly", "readonly");           
     }).error(function(jqXHR, textStatus, errorThrown) {
        if(jqXHR.status == 404){
            //NOTFOUND
        }
    })
});

答案 1 :(得分:0)

做这样的事情:

$.ajax({
    url: '/ContactUs/GetMember/',
    data: {id: $('#memberid').val()},
    success: function(data){
        //check if any results returned
        if(data.result.length > 0){
            $('#FName').val(data[0].M_Fname).attr("readonly", "readonly");
            $('#LName').val(data[0].M_Lname).attr("readonly", "readonly");
            $('#Email_Id').val(data[0].M_Emailid).attr("readonly", "readonly");
            $('#Cno').val(data[0].M_ContactNo).attr("readonly", "readonly");
        }else{
            //send message to user if results are 0
            alert('no results were returned');
        }
    }
})