Reputation: 190
I want to auto-fill textbox if member id exists in database. Otherwise I want to disaplay "please enter correct Member-Id"
Here is my Jquery Code:
$('#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);
});
})
And here is my Controller:
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);
}
Here I am returning JsonResult and using getJSON to get data from database.
Upvotes: 1
Views: 193
Reputation: 803
In my opinion the controller action does exactly what it should.. return nothing if nothing is found. Just check for an empty response on the client side..
$('#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
}
});
})
Alternatively you can return an Http 404 (Not Found) and handle the error...
Controller:
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
}
})
});
Upvotes: 2
Reputation: 2201
Just do something like this:
$.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');
}
}
})
Upvotes: 0