Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:29:30+00:00 2026-05-30T20:29:30+00:00

I have got this method in my controller. public string GetTime(string zone) { DateTime

  • 0

I have got this method in my controller.

 public string GetTime(string zone)
    {
        DateTime time = DateTime.UtcNow.AddHours(offsets[zone]);
        return string.Format("<div>The time in {0} is {1:h:MM:ss tt}</div>", zone.ToUpper(), time);
    }

    private Dictionary<string, int> offsets = new Dictionary<string, int> { { "utc", 0 }, { "bst", 1 }, { "mdt", -6 }};

This is my html:

@{
    ViewBag.Title = "Statistics";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<html>
<head runat="server">
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")"
    type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" 
    type="text/javascript"></script>
</head>
<body>
    <h2>
        Statistics</h2>
    <h2>
        What time is it?</h2>
    <p>
        Show me the time in:<br />
        @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br />
        @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" })
        <br />
        @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" })
        <br />
    </p>
    <div id="myResults" style="border: 2px dotted red; padding: .5em;">
        Results will appear here
    </div>
    <p>
        This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC)
    </p>
</body>
</html>

The problem is that the time doesnt appear in the div element..but the time presented on a blank page (meaning there is a postback instead of an ajax call). Why does that happen?

This is how the html of the links loaded look like:

    <script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>

<script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script> 

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

<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:29:31+00:00Added an answer on May 30, 2026 at 8:29 pm

    You have included a Layout but in your view, you have an entire <html> document and I suppose that you end up with some very broken HTML at the end.

    Here’s how your view could look like if you don’t want to use a layout:

    @{
        ViewBag.Title = "Statistics";
        // Explicitly specify that we don't use any layout because
        // this view already contains the entire html document
        Layout = null;
    }
    
    <html>
    <head>
        <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    </head>
    <body>
        <h2>
            Statistics</h2>
        <h2>
            What time is it?</h2>
        <p>
            Show me the time in:<br />
            @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br />
            @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" })
            <br />
            @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" })
            <br />
        </p>
        <div id="myResults" style="border: 2px dotted red; padding: .5em;">
            Results will appear here
        </div>
        <p>
            This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC)
        </p>
    </body>
    </html>
    

    The only scripts you need is jquery and jquery.unobtrusive-ajax. Also don’t use any runat="server" attributes in razor.

    and if you want to use the layout:

    @{
        ViewBag.Title = "Statistics";
    }
    
    <h2>
        Statistics</h2>
    <h2>
        What time is it?</h2>
    <p>
        Show me the time in:<br />
        @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br />
        @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" })
        <br />
        @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" })
        <br />
    </p>
    <div id="myResults" style="border: 2px dotted red; padding: .5em;">
        Results will appear here
    </div>
    <p>
        This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC)
    </p>
    

    Once again don’t forget the 2 scripts in your layout.

    And a final remark: by convention all controller actions should return ActionResults, so:

    public string GetTime(string zone)
    {
        DateTime time = DateTime.UtcNow.AddHours(offsets[zone]);
        return Content(string.Format("<div>The time in {0} is {1:h:MM:ss tt}</div>", zone.ToUpper(), time));
    }
    

    Finally make sure you don’t use any Microsoft*.js script in your pages. Those are obsolete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I've got problem with foreach statement. I have my controller method: public ActionResult
I currently have a controller method that returns a JsonResult: public JsonResult EditField(int FieldID,
I got a plain spring3 web project set up and have a controller method
I have got this error when using Enterprise Library 3.1 May 2007 version. We
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got this code and I am trying to understand the convention followed,
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
I have got some javascript code and I'd like to convert this to C#.
I have got a piece of code, that should countdown some number (in this
So I've got this UpdatePanel. Inside it I have a nifty little jQuery scroll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.