0

i want to call a jquery function in a MVC 4 razor syntax foreach loop,function must be called in every loop

here's my updated code

@foreach (var item in Model) {
<table>
<td>
          <div id="date">  

          <input id="datei" type="hidden" value="@Html.DisplayFor(modelItem => item.hire_date)" />
          </div>
         <div id="datedis"></div>
        <script >
           GetDate()

        </script>

    </td>

this is Getdate function

 var GetDate = function()
{
    var date2 = new Date("2014-11-01")
    var dt = $("#datei").val();
    var dtx = new Date(Date.parse(dt));
    var datediff = date2 - dtx;

    var years = datediff / 1000 / 60 / 60 / 24 / 365;
    var months = datediff / 1000 / 60 / 60 / 24 / 31;

    var yeardisplay;
    var monthdisplay;

    if (years > 0) {
        yeardisplay = Math.floor(years);
        monthdisplay = Math.floor(months);

        $("#datedis").html(yeardisplay + "Years," + " " + monthdisplay + " Months");
    } else {
        monthdisplay = Math.floor(months);

        $("#datedis").html(monthdisplay + " Months");
    }
}
4
  • What is the Javascript function? What does it do, etc. Commented Apr 16, 2014 at 6:32
  • it is used for format a datetime Commented Apr 16, 2014 at 6:37
  • i have found a another way to do it..this way is more harder i just modified the data in controller Commented Apr 16, 2014 at 11:53
  • thats better anyway, embedding scripts calls in this manner is a horrible practice. Commented Apr 17, 2014 at 14:00

2 Answers 2

1

do like this:

@foreach (var item in Model) 
{
    <table>
       <tr>
           <td>
           </td>
       </tr>
    </table>

    <script>                

    GetDate();

    </script>  

}
Sign up to request clarification or add additional context in comments.

5 Comments

i got this error when i run above code ReferenceError: GetDate is not defined
i have implemented the getdate function below in a script tag
it must defined before calling place it on top of view
it worked but only for 1st loop from 2nd loop functoin not working i have found a another way thanks
the function will be called in every loop the thing you did is in definition of it ?
0

Rather then mixing the script with html. I would recommend you to write out the html and have the jQuery/javascript to iterate through it when the page is loaded. Let me know if you need any further help with it.

DEMO

Look at the demo, for html structure. But this is basically what I mean. You would have to modify it for you GetDate(), sorry for my lack of time to do it.

$(document).ready(function () {
    var obj = $('.container .date-container');

    $.each(obj, function (i, item) {
        //item here is each date-container (your case table)
        //so you can do what you like with it here
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.