locked
Hello how to pass a variable to a method in the function? RRS feed

  • Question

  • User-1985277586 posted

    Hello, tell me, please, how to pass a variable id in _Method (id) method and assign a value variable vrm_start to a variable genMessage.
    Now the errors
    - name "id" does not exist in the current context.
    - name "genMessage" does not exist in the current context.

    Poluchenie_Vremeni_Ostanovki of the controller.
    function generateMessage (id) - js

    function generateMessage (id)
    {
      var genMessage = '111';
      switch (id) 
      {
        case 'M': genMessage += ", 222.";
        break;
        case 'P': 
        @{
           MvcApplication1.Models.ListModel List_ost = new MvcApplication1.Models.ListModel();
           string vrm_start = List_ost.Poluchenie_Vremeni_Ostanovki(id);
           genMessage += vrm_start;
         }
        break;
        case 'I': genMessage += ", 333.";
        break;
        default:
        break;
      }
     return genMessage;
    }
    
    
    public string Poluchenie_Vremeni_Ostanovki(string id) 
    {
      return "test";
    }

    Tuesday, July 26, 2016 2:21 PM

All replies

  • User1313602441 posted
    First, it's best to place this function as a Function helper in the App Code folder rather than using it directly in a CSHTML file.
    Second, the reason you are getting those errors is because the variables either do not exist as of the time the function calls them or they are unreachable. From your code, the variable won't be created before function call since it's placed at the bottom of the page.

    Place this code at the top of the file:
    public string Poluchenie_Vremeni_Ostanovki(string id) {
    return "test";
    }
    Third, get the line number of the "genMessage" error.
    And your code looks somehow confusing and scattered for your ownreadability purposes, you should rewrite it.
    Tuesday, July 26, 2016 6:29 PM
  • User-1985277586 posted

    If you do not pass a variable in Poluchenie_Vremeni_Ostanovki, so it works

    @{
    MvcApplication1.Models.ListModel List_ost = new MvcApplication1.Models.ListModel ();
    string vrm_start = List_ost.Poluchenie_Vremeni_Ostanovki ();
    }
    genMessage + = "@vrm_start";



    How to pass a variable id in Poluchenie_Vremeni_Ostanovki

    string id = 1;

    @{
    MvcApplication1.Models.ListModel List_ost = new MvcApplication1.Models.ListModel ();
    string vrm_start = List_ost.Poluchenie_Vremeni_Ostanovki (id);
    }



    Tuesday, July 26, 2016 6:58 PM
  • User-1985277586 posted

    Now the question looks like this:
    how to pass a variable id in _method (id) method of the controller, called from js function?
    Now error:

    - name "id" does not exist in the current context.

    <script type="text/javascript">
    function generateMessage (id)
    {
       var genMessage = '111';
       switch (id) 
       {
          case 'M': genMessage += ", 222.";
          break;
         case 'P': 
        @{
               MvcApplication1.Models.ListModel List_ost = new MvcApplication1.Models.ListModel();
               string vrm_start = List_ost._method(id);
           }
       genMessage += "@vrm_start";
       break;
       case 'I': genMessage += ", 333.";
       break;
       default:
       break;
       }
      return genMessage;
    }
    </script>

    controller

    namespace MvcApplication1.Models
    {
      public class ListModel
        {
          public string _method(string id)
           { 
             return "test";
           }
        }
    }



    Wednesday, July 27, 2016 5:04 AM
  • User-1985277586 posted

    I formulate a question, and the answer he found - in any way, the js is performed on the client.

    Wednesday, July 27, 2016 5:20 AM