skip to Main Content

in my MVC View I would like to do something like limiting model objects only to objects which property "Date" is greater than date from input calendar.
In scripts sections in function drawChart() I have added
var start = new Date(document.getElementById("start").value); but when I try to get this this value I get an error "The name ‘start’ does not exists in the current context". This is what I want to do:

            var start = new Date(document.getElementById("start").value);
            var test = @Model.Where(s => s.Date > start)
            var data = google.visualization.arrayToDataTable([
                ['Date', 'Entries'],
                @foreach (var obj in test)
        {
                <text>  [new Date(@obj.Date.Year, @obj.Date.Month - 1, @obj.Date.Day), @obj.Count], </text>
        } ]);

How should I do it?

3

Answers


  1. As Jack explained you are trying to access a javascript variable (which is client side) within vs code (which is server side). Every expression that is adjacent to a @ sign compiles at server side. You may send that date variable from client to server by putting it in a html form element, then submit it to the pagemodel.

    Login or Signup to reply.
  2. try viewbag instead, i tried something like that in my code, and it worked fine.

     @ViewBag.abc = 'DAN';
                var s = @ViewBag.abc;
                alert(s);
                var test = Model.Where(s => s.BatchName == @ViewBag.abc )
    

    in your code try doing it like this:

    @ViewBag.abc = new Date(document.getElementById("start").value);
        var s = @ViewBag.abc;// this part and alert is just to check the value inside viewbag. 
        alert(s);// comment both these lines .
        var test = Model.Where(s => s.Date > @ViewBag.abc )
    
    Login or Signup to reply.
  3. Try this it worked for me. Thanks

    @ViewBag.abc = new Date(document.getElementById("s_date").value);
        var axen = Model.Where(s => s.S_Date > @ViewBag.abc )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search