Checking value in database on link click
I have an href in my asp.net MVC3 Razor view and I want to check on click of this razor view:
If there exist a record in database, If exists, then show alert, otherwise print some value on page.
link html is like this:
<a class="orphan item subtext" href="#" id="lnkfreereport">
<span>
Order Your Fre开发者_运维百科e Report
</span>
</a>
Please suggest how can I do this using asp.net MVC
Please refer AjaxHelper class ActionLink method
http://msdn.microsoft.com/en-us/library/dd493106.aspx
AJAX?
@Html.ActionLink(
"Order Your Free Report",
"CheckExists",
"SomeControllerName",
null,
new { id = "checkExists" }
)
and then in a separate javascript file:
$(function() {
$('#checkExists').click(function() {
$.getJSON(this.href, function(result) {
if (result) {
alert('the record exists');
}
});
return false;
});
});
and then you could have a controller action which checks whether the given record exists and returns JSON:
public ActionResult CheckExists()
{
bool exists = ...
return Json(exists, JsonRequestBehavior.AllowGet);
}
精彩评论