如何在ASP.NET MVC中刷新包含一个自定义HTML帮助器的div? [英] How to refresh a div which contains one custom HTML helper in ASP.NET MVC ?
本文介绍了如何在ASP.NET MVC中刷新包含一个自定义HTML帮助器的div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在ASP.Net MVC中刷新包含一个自定义HTML帮助程序的Div?
助手将显示时间。当点击一个按钮时,div必须刷新&时间应该改变。但它不起作用,有没有人能在这方面帮助我?
我尝试了什么:
创建自定义助手&在Div中使用它。现在我想在点击按钮后刷新Div。
How to refresh a Div which contains one Custom HTML Helper in ASP.Net MVC ?
The helper will show the time. When a button is clicked then the div has to refresh & the time should change. But it is not working, can anyone help me in this regard ?
What I have tried:
Create Custom Helper & used it inside a Div. Now I want to refresh the Div after button click.
推荐答案
试试这个
创建一个动作方法来返回时间字符串。 />
控制器:
try this
Create an Action Method to return the time string.
Controller:
public ActionResult GetTime()
{
return Json(DateTime.Now.ToLongTimeString());
}
查看:
View:
<div id="div1">
@DateTime.Now.ToLongTimeString()
</div>
<button onclick="refreshTime()">Refresh</button>
<script>
function refreshTime() {
var url = '@Url.Action("GetTime","Home")' // 'GetTime' action in 'Home' Controller
.ajax({
url:url,
data:{},
type : post,
success: function (时间){
.ajax({ url: url, data: { }, type: "post", success: function (time) {
(' #div1'跨度>)HTML(时间)。
},
错误: function (){
alert('' 错误');
}
});
}
< / script >
('#div1').html(time); }, error: function () { alert('error'); } }); } </script>
这篇关于如何在ASP.NET MVC中刷新包含一个自定义HTML帮助器的div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文