Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 6-Jun-16 3:33am
Comments
F-ES Sitecore 6-Jun-16 8:14am    
You'll need to post your code.
Ayan Chaudhuri 6-Jun-16 8:30am    
<div id="div1">
@DateTime.Now.ToLongTimeString()
</div>

Now I want to refresh the Div 'Div1' & I wish the content will have the latest time inside that Div.
Ayan Chaudhuri 6-Jun-16 8:15am    
<div id="div1">
@DateTime.Now.ToLongTimeString()
</div>
Ayan Chaudhuri 6-Jun-16 8:17am    
Now I want to refresh the Div 'Div1' & I wish the content will have the latest time inside that Div.
Karthik_Mahalingam 6-Jun-16 9:14am    
use ajax instead.

1 solution

try this
Create an Action Method to return the time string.
Controller:

C#
public ActionResult GetTime()
       {
           return Json(DateTime.Now.ToLongTimeString());
       }


View:

HTML
<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 (time) {
                $('#div1').html(time);
            },
            error: function () {
                alert('error');
            }
        });
    }

</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900