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 7913471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:51:09+00:00 2026-06-03T13:51:09+00:00

can anyone please tell me how to call the function GetDuration from within the

  • 0

can anyone please tell me how to call the function GetDuration from within the following context:

@{
...
...
<div>  GetDuration(@item.StartDate, @item.EndDate) </div>
...
...
}

The function GetDuration() dynamically creates another DIV inside the parent DIV mentioned in my context.

I have 2 problems:

  • I do not know how to call my function GetDuration() from the spot shown in the code
  • startdatum.Month.ToString("MMMM"); returns "MMMM" instead of the name of the month

Thanks in advance!

This is my code:

<script type="text/javascript">

function GetDuration(startdatum, einddatum) {
    var maand1 = startdatum.Month.ToString("MMMM");
    var maand2 = einddatum.Month.ToString("MMMM");
    var duration = "Date: ";
    if (maand1 == maand2) {
        duration += item.Startdate.Day.ToString()
            + " - " + item.Enddate.Day.ToString()
            + " " + maand1
            + " " + item.Startdate.Year.ToString();
    }
    else {
        duration += item.Startdate.Day.ToString()
            + item.Startdate.Month.ToString("MMMM")
            + " - " + item.Enddate.Day.ToString()
            + " " + item.Enddate.Month.ToString("MMMM")
            + " " + item.Startdate.Year.ToString();
    }
    var dynDiv = document.createElement("div");
    dynDiv.class = "HP_DateDiv";
    dynDiv.innerHTML = duration;
    document.body.appendChild(dynDiv);
}

</script>


    @{  string prevYear = "";

    foreach (var item in ViewData.Model) {

        string year = item.StartDate.Year.ToString();
        if (year != prevYear)
        {
            <div class="contentheader"> 
                @year Formula 1 Tickets
            </div>

            prevYear = year;  
        }
        <div class="HP_eventBodyDiv">
            <div class="HP_picDiv" style = "width:100px; height:100px;">
                <a href="http://www.cssdrive.com/" class="highlightit"><img  src="../../Content/Pics/auto.jpg" alt="oto" height="50" width="50"/></a>
            </div>
            <div class="HP_eventDiv">
                <div class="HP_eventNameDiv">@item.Name
            </div>
<!--This is the spot to call my function with something like "GetDuration(@item.StartDate, @item.EndDate)"-->
                <div class="HP_viewBuyBtnsDiv">
                    <input id="btn_View" type="button" value="View Event" class="contentbtn"/>
                    <input id="btn_Buy" type="button" value="Buy Tickets" class="contentbtn"/>
                </div>
            </div>
        </div>            

        };
     }
  • 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-03T13:51:11+00:00Added an answer on June 3, 2026 at 1:51 pm

    I’m going to see if I can explain this a bit for you.
    Razor Syntax allows us to do some great stuff like your doing on your view. The Ability to iterate through a collection on your view is really great, and can give some good flexibility to create dynamic pages.
    They server is actually creating the Razor view with the syntax that your providing and then sends the build HTML page to the Client. That includes the script tags. So what your trying to do doesn’t realty work. You can’t iteratively call JavaScript Function in the @foreach Like that because the page hasn’t been rendered yet.
    You should only think of using Javascript once the Razor View has been completely formed. Then you can use JavaScript to edit the DOM.

    So my suggestion is what the EDIT says. Let the sever calculate that property and send it with the Model.

    EDIT:

    Wait after looking at updated Code I think your going about this the wrong way. I Don’t understand why you are using javascript to calculate your Duration. You should edit your model that you are passing through to contain the duration especially if its a calculated field like that. then you can just display the Duration just as any other property.

    EDIT 2:

    public class MyNewClass
    {
    public DateTime StartDate {get;set;}
    public DateTime EndDate {get;set;}
    public int Duration
    {
    get{
    //Do your Logic Here with startdate and enddate
    }
    set;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please tell me how can I pass a list item from one
Can anyone please tell me how to write a small div container in HTML
I want to make a drop-down list from scratch. Can anyone please tell me
Can anyone please tell me what r we actually meaning by the following statement
Can anyone please tell me, what exactly is meant by registering a callback function
can anyone please tell about showing message box from MVC controller? The scenario is
Can anyone please tell me how I can get the jQuery Validator to call
Can anyone please tell what is change request management tool and what is involved
Can anyone please tell me why compiling a C# application with a .cs file
Can anyone please tell me when should I use MessageContracts and when should I

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.