Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Issues in ajax call in MVC

Issues in ajax call in MVC

Scheduled Pinned Locked Moved C#
htmlasp-netsalesjsonarchitecture
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #1

    Hello, I am new in MVC. I have one product table which has ProductID, ProductName and ProductRate. I have one dropdown list which has all the ProductName. Now I am trying to select the productname and upon this select, it should fill my textbox with ProductRate. Code is as below but it is not doing anything and no errors. Please help.

    function GetPrice(\_this) {
    
        var x = document.getElementById("productID"), selectedValue = x.value;
        alert("\_this" + \_this.SelectedValue);
        var pid = selectedValue;
        alert(pid);
        var url = '@Url.Action("GetPrice","Sales")';
        alert(url);
              $.ajax({
                type:"POST",
                url: 'Sales/GetPrice',
                contentType: "application/json; charset=utf-8",
                data:  {ProductId: pid },
                cache:false,
                dataType: json,
                async: true,
                processData:false,
                success: function (data)
                {
                    alert("yay1");
                },
                failure: function (response) {
                    alert("Fail");
                }
    
        });
    
    };
    

    @using (Html.BeginForm())
    {
    @Html.ValidationSummary(true)

            @Html.LabelFor(model => model.ProductId)
    

    @Html.DropDownListFor(model => model.ProductId, new SelectList(ViewBag.ProductList, "ProductId", "ProductName" ), "Select Product", new { id = "productID", onchange="GetPrice(this);"})
    @Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })

            @Html.LabelFor(model => model.Rate)
        
    
        
    
            @Html.EditorFor(model => model.Rate, new { id = "idRate"})
            @Html.ValidationMessageFor(model => model.Rate)
    

    \

    }

    Here is my Controller "SalesController.cs" code:

    [HttpPost]
    public JsonResult GetPrice(int ProductId)
    {
    return Json("");
    }

    Dhyanga

    D 1 Reply Last reply
    0
    • D Dhyanga

      Hello, I am new in MVC. I have one product table which has ProductID, ProductName and ProductRate. I have one dropdown list which has all the ProductName. Now I am trying to select the productname and upon this select, it should fill my textbox with ProductRate. Code is as below but it is not doing anything and no errors. Please help.

      function GetPrice(\_this) {
      
          var x = document.getElementById("productID"), selectedValue = x.value;
          alert("\_this" + \_this.SelectedValue);
          var pid = selectedValue;
          alert(pid);
          var url = '@Url.Action("GetPrice","Sales")';
          alert(url);
                $.ajax({
                  type:"POST",
                  url: 'Sales/GetPrice',
                  contentType: "application/json; charset=utf-8",
                  data:  {ProductId: pid },
                  cache:false,
                  dataType: json,
                  async: true,
                  processData:false,
                  success: function (data)
                  {
                      alert("yay1");
                  },
                  failure: function (response) {
                      alert("Fail");
                  }
      
          });
      
      };
      

      @using (Html.BeginForm())
      {
      @Html.ValidationSummary(true)

              @Html.LabelFor(model => model.ProductId)
      

      @Html.DropDownListFor(model => model.ProductId, new SelectList(ViewBag.ProductList, "ProductId", "ProductName" ), "Select Product", new { id = "productID", onchange="GetPrice(this);"})
      @Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })

              @Html.LabelFor(model => model.Rate)
          
      
          
      
              @Html.EditorFor(model => model.Rate, new { id = "idRate"})
              @Html.ValidationMessageFor(model => model.Rate)
      

      \

      }

      Here is my Controller "SalesController.cs" code:

      [HttpPost]
      public JsonResult GetPrice(int ProductId)
      {
      return Json("");
      }

      Dhyanga

      D Offline
      D Offline
      Dhyanga
      wrote on last edited by
      #2

      I am able to fix it. Please disregard this post.

      Dhyanga

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups