Issues in ajax call in MVC
-
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
-
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