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

  • SEARCH
  • Home
  • 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 9061689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:33:00+00:00 2026-06-16T15:33:00+00:00

I am working with Asp.Net MVC4, I need to retrieve the date and time

  • 0

I am working with Asp.Net MVC4, I need to retrieve the date and time from the server and not the client. To restore them when I must click a button in the view, for example the name of the button “Nuevo” and from the view so I defined, the event is called by javascript in Action define the controller (Home) and ActionResult (Nuevo):

<script type= "text/javascript">
    function Nuevo() {
        $.ajax({
           url: '@Url.Action("Nuevo", "Home")',
           type: 'POST',
           contentType: 'application/json; charset=utf-8',
           dataType: 'json',
           data: JSON.stringify({ }),
           success: function () {}
        });
     }
</script>

On receipt the data controller in the New ActionResult as follows:

[HttpPost]
public ActionResult Nuevo()
{
     Persona persona = new Persona();
     persona.Fecha = DateTime.Now;
     persona.Hora = DateTime.Now;
     return View(persona);
}

This is my view, I use typed views:

@model MvcJavaScript.Models.Persona
<script type= "text/javascript">
    function Nuevo() {
        $.ajax({
            url: '@Url.Action("Nuevo", "Home")',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify({}),
            success: function (data) {
            }
        });
    } 
</script>
<h2>Registro persona</h2>
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "formPersona" })){ 
   <table cellpadding="4" class="td.headerTabsColor">
     <tbody>
       <tr>
             <td><label>Fecha : </label>@Html.TextBoxFor(u => u.Fecha, new { sololectura = true })</td> 
       </tr> 
       <tr>
         <td><label>Sexo : </label>@Html.TextBoxFor(u => u.Sexo, new { style = "width:225px", sololectura = false })</td>
       </tr>
       <tr>
            <td><label>Hora : </label>@Html.TextBoxFor(u => u.Hora, new { sololectura = true })</td>  
       </tr>
     </tbody>
  </table>
}

What I need to do is to insert a new record (by clicking on the button “Nuevo”) I load the default server time and date in different textbox.

Running this example enters the New ActionResult but to return to the data to view the TextBox is void, I tried other fields and the same result.

Any suggestions or help with this problem.

regards

Ricardo

  • 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-06-16T15:33:01+00:00Added an answer on June 16, 2026 at 3:33 pm

    There are basically two different routes you usually take when creating an AJAX action: letting the server render the HTML, or just sending data back to the browser and let the browser render the HTML. The code you have posted is a mixture of the two – that’s why it’s not working. The jQuery AJAX call is expecting JSON data back from the server, but the server is sending the HTML rendered by the Views\Home\Nuevo.cshtml view. Let’s look at what these two different approaches might look like.

    Server-Rendered Approach

    You need to add an HTML element that will display the result. We will call it NuevoResult. And we also need some code that will put the response there. The easiest way is jQuery’s .html() method.

    <div id="NuevoResult"></div>
    <script type= "text/javascript">
        function Nuevo() {
            $.ajax({
                url: '@Url.Action("Nuevo", "Home")',
                type: 'POST',
                // ... also 'contentType' and 'data' if you're actually sending anything to the server...
                dataType: 'html',
                success: function (data) {
                    $('#NuevoResult').html(data);
                }
            });
        }
    </script>
    

    We also need a Views\Home\Nuevo.cshtml view for the server to render. It might look like:

    @model MyCoolNamespace.Persona
    
    <h3>New person created!</h3>
    <p>Created on @string.Format("{0:d}", Model.Fecha) at @string.Format("{0:t}", Model.Hora).</p>
    

    This is all the HTML we want to return from this action. We don’t want it to be wrapped in any layout. To do this, we need to make sure we return PartialView(persona) instead of return View(persona).

    Browser-Rendered Approach

    For the browser rendered approach, we’ll go ahead and have the HTML ready on the browser, but hidden. We’ll fill it in with the correct information and display it when we receive a response from the server.

    <div id="NuevoResult" style="display:none">
        <h3>New person created!</h3>
        <p>Created on <span id="Fecha"></span> at <span id="Hora"></span>.</p>
    </div>
    <script type= "text/javascript">
        function ParseJSONDateTime(value) {
            // from http://stackoverflow.com/questions/206384/how-to-format-a-json-date/2316066#2316066
            return new Date(parseInt(value.substr(6)));
        }
        function Nuevo() {
            $.ajax({
                url: '@Url.Action("Nuevo", "Home")',
                type: 'POST',
                // ... also 'contentType' and 'data' if you're actually sending anything to the server...
                dataType: 'json',
                success: function (data) {
                    $('#Fecha').text(ParseJSONDateTime(data.Fecha).toLocaleDateString());
                    $('#Hora').text(ParseJSONDateTime(data.Hora).toLocaleTimeString());
                    $('#NuevoResult').show();
                }
            });
        }
    </script>
    

    And then in the MVC action, use return Json(persona) to send the data back to the browser.

    A few more notes…

    The .NET DateTime structure holds both date and time information, so there’s no need to have separate Fecha and Hora properties. Consider replacing with a single CreatedTimestamp property.

    If you’re still having trouble, Firefox’s Firebug extension, Internet Explorer’s Developer Tools, and Chrome’s Developer Tools can be very helpful in figuring out what is wrong, allowing you to see exactly what was returned from the server.

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

Sidebar

Related Questions

I was working on a migration job from Asp.net MVC4 beta to Asp.net MVC4,
I am working on a project ASP.net 4, MVC4(RC) using visual studio 11. I
I am working on asp.net application and I have generated entity model (edmx) from
I am working on ASP.net Webform application where i need to block certain dates
I'm working with ASP.Net MVC4, I customize my login, this is ok, I would
I'm working on Asp.net MVC4 project using Visual Studio 2012. When there's an error
I am working on my first ASP.NET MVC 4 app. The client is deploying
I am working with ASP.Net MVC4 and I am getting the following error while
I'm working on a new asp.net mvc4 project using Visual Studio 2011 beta and
I am working with a ASP.NET MVC4 application. I have created a view model

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.