【问题标题】:How to refresh a div without reloading the whole pageHow to refresh a div without reloading the whole page
【发布时间】:2022-12-02 11:07:18
【问题描述】:

I have an ASP.Net Core MVC project and in it I implemented a DateTime, and I need that when I click on the button it reloads the div with the new date that I inserted, but without loading the entire page, just that div.

【问题讨论】:

  • Hi @essrrom, I have share a simple demo below. It is better for you to share your code for more efficient help.

标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-core-mvc


【解决方案1】:

I need that when I click on the button it reloads the div with the new date that I inserted, but without loading the entire page, just that div.

You need use AJAX to achieve your requirement.

Here is a simple demo about how to update the div without page reloading:

View

<div>
    <input type="datetime-local" id="MyDate" />
</div>

<input type="button" onclick="ChangeDatetime()" value="Change" />
@section Scripts
{
    <script>
        function ChangeDatetime()
        {
            $.ajax({
                url: "/Home/Update?MyDate=" + $("#MyDate").val(),
                method:"Get",
                success:function(data){
                    $("#MyDate").val(data);
                }
            })
        }
    </script>
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    public string Update(DateTime MyDate)
    {
        MyDate = DateTime.Now;   //modify the datetime here
        //the browser only accepts yyyy-MM-ddThh:mm format for `datetime-local` type
        return MyDate.ToString("yyyy-MM-ddThh:mm");
    }
}

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 2022-12-26
    • 2022-12-27
    • 2022-12-27
    • 2022-12-02
    • 2022-12-02
    • 2022-12-02
    • 2022-12-02
    • 2022-12-01
    相关资源
    最近更新 更多