I'm still getting my feet wet experimenting with MVC3. I haven't found a good resource to get me started. I was wondering if I could consume a webservice that returns a string when a user is done typing in a zip code?
I have consumed a webservice using json but in a ASP.NET web application. I was able to return an Franchise based on the user's entered zip code. I was wondering if I could do the same thing in MVC?
How to convert the following to work in MVC:
// Html code in my Test.aspx page
<input id="txtZipCode" type="text" onchange="javscript:changed(this.value)" />
<div id="Result"></div>
Here my javascript code:
function changed(value) {
$.ajax({
type: "POST",
url: "Test.aspx/GetData",
data: "{'zipcode': '" + value + "'}",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
},
error: function () {
$("#Result").text('Failed');
}
});
}
Here's my code behind:
[System.Web.Services.WebMethod]
public static string GetData(string zipcode)
{
srvc.bbcs tmp = new srvc.bbcs (); // consume test webservice
string code = tmp.GetFETerrByZip(zipcode);
return code;
}