views:

31

answers:

2

Controller action:

    public ActionResult Index()
    {

        ViewData["sample"] = DateTime.Now.ToLongTimeString();
        Thread.Sleep(3000);
        return View();
    }

View page:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<div id="divId"><%= Html.Encode(ViewData["sample"])%></div>

 <script type="text/javascript">var event = new Object(); $get("panelOneForm").onsubmit();</script>
 <%using (Ajax.BeginForm("Index", "Proba", new AjaxOptions() { UpdateTargetId = "divId" }, new { id = "panelOneForm" })) { } %>

</asp:Content>

I try to make auto update View but failed. What is wrong?

A: 

Did you import the microsoft ajax libraries ?

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

here is a tutorial. Do you have firebug? If so, try to see if the libraries are loaded correctly, and no errors where detected while executing

Nealv
yes I have firebug and import ajax libraries
Ognjen
I have this error on MisrosoftAjax.debug.js: Sys.ArgumentTypeException: Object of type 'Sys._Application' cannot be converted to type 'Sys._Application'. Parameter name: instance[Break on this error] if (!this.isInstanceOfType(instanc...e', Object.getType(instance), this);
Ognjen
Do you have any other ajax in the page that is making a call ? Have you referenced the script in your web.config ?
Nealv
I referenced script in site.master
Ognjen
In web.config I have this: <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Linq" /> <add namespace="System.Collections.Generic" /> </namespaces>
Ognjen
it is strange that you are having this in mvc. I can't figure it out. I would use jquery, sorry
Nealv
A: 

You could define an invisible submit button inside the form:

<div id="divId"></div>

<%using (Ajax.BeginForm("Index", "Proba", new AjaxOptions() { UpdateTargetId = "divId" })) { %>
    <input type="submit" id="btnSubmit" style="display:none;" />
<% } %>

<script type="text/javascript">
    // Make sure you put this script after the form
    // so that the button is loaded into the DOM before
    // manipulating it
    $get("btnSubmit").click();
</script>

Also the Index action on the Proba controller that you are invoking asynchronously needs to return only a partial view or directly some content. Notice that ViewData is no longer used neither in the controller action nor in the resulting div:

public ActionResult Index()
{
    // You probably want to remove the next line before shipping your
    // application in production as it is not good to stall the thread for 3s
    Thread.Sleep(3000);
    return Content(DateTime.Now.ToLongTimeString(), "text/plain");
}
Darin Dimitrov
I try with your code but problem is still there, date and time not update automaticaly
Ognjen
This $get("btnSubmit").click(); click only one time, why
Ognjen
@Ognjen, maybe because you are calling it only once.
Darin Dimitrov
How will I adjust to call every 10 seconds ie auto refresh
Ognjen
By using the `setInterval` function: `window.setInterval(function() { $get("btnSubmit").click(); }, 10000);`
Darin Dimitrov
Now is fine, thank you very much Darin
Ognjen