Hi i am basically new to Ajax and having a tough time writing it..
I want to assign src value to Iframe which is fetched from the database
Table is HobbyMasters
HobbyName
HobbyUrl
I write a function and fetch the url from the table now when i click on Hobbyname which is displayed as link i want that url to be loaded inside the Iframe.
Initially i wrote a javascript:
<script type="text/javascript">
$(document).ready(function () {
$('a').click(function (e) {
e.preventDefault();
$('iframe').attr('src', "Dancing");
});
}); </script>
But here the src is static and through javascript i am not able to assign fetched value from databse to src attribute
So thought of writing Ajax..
I have tried something but it is incomplete.Please Help me with this:
<script type="text/javascript">
$(document).ready(function () {
$('a').click(function (e) {
e.preventDefault();
var filename = $(this).text();
var Hobbyurl = '@Url.Action("FetchUrlByHobbyName")';
$.ajax({
type: "POST",
url: Hobbyurl ,
data: { data: filename },
success: function (returndata) {
Here i want to assign the fetched src from function FetchUrlByHobbyName to Iframe src
$('iframe').attr('src', filename);
}
});
});
Function inside the Controller:
[HttpPost]
public ActionResult FetchUrlByHobbyName(string Hobbyurl)
{
HobbyMasters hobbymaster = new HobbyHomeService().FetchHobbyMasterByHobbyName(Hobbyurl);
string url=hobbymaster.InformationUrl;
if (HttpContext.Request.IsAjaxRequest())
return Json(new{src=url});
return View();
}