Reputation: 319
This is my controller
public ActionResult Index()
{
return View();
}
public static string GetCuttentTime()
{
return DateTime.Now.ToLongTimeString();
}
}
This is my view
@{
ViewBag.Title = "Index";
}
<div id="div1">
@Temp.Controllers.HomeController.GetCuttentTime()
</div>
<input type="button" value="Refresh" />
I want to refresh the current time on button click. Please Help
Upvotes: 1
Views: 7402
Reputation: 33306
You can use jQuery to do this as follows:
$("input" ).click(function() {
$("#div1").load('@Url.Action("GetCuttentTime")');
});
You would need to change your controller action to this:
public ActionResult GetCuttentTime()
{
return Content(DateTime.Now.ToLongTimeString());
}
Also change your div to this:
<div id="div1">
</div>
Removing your controller code.
Screen shot
Upvotes: 1
Reputation: 435
1) you returns Json: return Json(model, JsonRequestBehavior.AllowGet);
2) you put returned Json object to the div's value: $("#" + area).text(data);
that's why you end up with json's representation inside div
You need to change it as follows:
1) assume you put html for that div to model's field called NewHtml
2) eptract html from the property of returned json: var returnedHtml = data.NewHtml;
3) use html() method instead of text(): $("#" + area).html(returnedHtml);
Upvotes: 1