开发者

problem asp.net mvc cannot load ajax content

开发者 https://www.devze.com 2023-02-04 01:18 出处:网络
in my view: <script src=\"../../Scripts/MicrosoftAjax.js\" type=\"text/javascript\"></script>

in my view:

 <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>

    <script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

    <script src="../../jquery-1.4.1-min.js" type="text/javascript"></script>

 <%= Ajax.ActionLink("Update", "Index", "Home", new AjaxOptions { UpdateTargetId = "time" })%>
        <br />
        <di开发者_JS百科v id="time">
            <% Html.RenderPartial("TimeControl"); %>
        </div>

in my controller:

[HttpGet]
        public ActionResult Index()
        {
            HomeModel model = new HomeModel(Request.Url.Host);

            // Normal Request
            if (!Request.IsAjaxRequest())
            {
                return View("Index", model);
            }

            // Ajax Request
            return PartialView("TimeControl");
        }

in my model:

public HomeModel()
        {
 Time = DateTime.Now;
        }

i think everything is ok, but if iam clicking update link, time will be not updated.. why? it schould be actual if i click update link


I suggest you do the following:

[HttpGet]
    public ActionResult Index()
    {
        HomeModel model = new HomeModel(Request.Url.Host);

        return View("Index", model);
    }


[HttpPost]
public ActionResult Index()
    {
        HomeModel model = new HomeModel(Request.Url.Host);

           // Ajax Request
        return PartialView("TimeControl");
    }

I think the problem might be that the AJAX request is a POST.


You may simply try to (1) remove [HttpGet] or (2) set the HttpMethod in the AjaxOptions to "GET".

Ajax.ActionLink("Update", "Index", "Home", new AjaxOptions { UpdateTargetId = "time", HttpMethod = "get" })

That should solve the problem.

(3) If it doesn't, check if you are using the correct overload. This should work:

Ajax.ActionLink("Update", "Index", "Home", null, new AjaxOptions { UpdateTargetId = "time" }), null)


Any of the options in my previous answers could solve your problem if the AJAX isn't working correctly with your application.

Buy in your example, you're also using something that wasn't quite clear to me.

You use a constructor overload that takes the url. And you use the default constructor to set the time. Is the constructor you use (the one that takes the url) also calling the parameterless constructor?

That constructor should do something like this:

public HomeModel(paramter.......) : this()
{
   // Whatever...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消