Click here to Skip to main content
15,885,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
want to display discounted price using onchange event of jquery in my asp.net mvc

but getting 500 internal server error.
GET http://localhost:9421/Products/GetDis?discount=1&productName=apple 500 (Internal Server Error)


View code is

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductPrice, new { @id = "prisValue" })
        </td>
       
            <td>

               @Html.DropDownList("Status", new List<SelectListItem>
                 {
                    new SelectListItem{ Text="Student Discount", Value = "1" },
                    new SelectListItem{ Text="Loyalty Discount", Value = "0" }
                 }, "Please select discount", new { onchange = "ChangeDis(this,'" + item.ProductName + "')" })
                
            </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>
@section scripts{
    <script>
        
        function ChangeDis(o, proName, targetPrice) {
            // Get the triggering element
           // var element = event.srcElement;
            var targetPrice= $("#prisValue").val();
            //var targetPrice = $(element).closest('td').prev('td');
            $.ajax({
                url: '@Url.Action("GetDis")',
                data: { "discount": o.value, "productName": proName , "tPrice":targetPrice},
                success: function (data) {
                    $(targetPrice).text(data);
                }
            });
        }
    </script>
}


What I have tried:

public ActionResult Index()
       {
           Customers customer = new Customers();

           return View(db.Products.ToList());
       }
       public JsonResult GetDis(string discount, string productName, decimal tPrice)
       {
           Customers customer = new Customers();

           var products = db.Products.ToList();

           decimal price;

           if (discount == "1")
           {

              customer.SetDiscountStrategy(new LoyalStudentDiscount());
               price = customer.ApplyDiscount(tPrice);
               //lblFinalPrice.Text = price.ToString("C");
           }
           if (discount == "0")
           {
               customer.SetDiscountStrategy(new LoyaltyDiscount());
               price = customer.ApplyDiscount(tPrice);
           }
           return Json(JsonRequestBehavior.AllowGet);
       }


How to disply discounted price on a view
Posted
Updated 13-Mar-17 4:17am
v2
Comments
F-ES Sitecore 13-Mar-17 10:34am    
is tPrice getting passed to the action? Anyway put a breakpoint in your action and follow it to see what is happening as the code inside is throwing an exception. You might be able to see the exception if you use the browser's dev tools also.

https://forums.asp.net/t/1982579.aspx?Using+the+browser+s+dev+tools+to+diagnose+ajax+problems+and+other+things+

Your method shouldn't be a GET either, it should at least be a POST, you also return no data to be consumed by the calling jQuery, it looks like the code is expecting you to send an updated price but you're not.
j snooze 13-Mar-17 17:17pm    
Agree with F-ES Sitecore. You're not passing in a target price to your ChangeDis javascript method in the onchange event of your view. Its going to be null if you don't pass anything.
sachin.vishwa90 14-Mar-17 2:57am    
There are few steps which you have to check.
1)put debugger in your javascript code where you are making an ajax call, check you are getting appropriate data in your script.
2)Put debugger in your action method and check you are getting appropriate data in your action method.
3) Make sure you are returning the object which is needed in your view
ex return Json(responseObject,JsonRequestBehavior.AllowGet); or JsonResult(responseObject,JsonRequestBehavior.AllowGet);
so key is debug your code and you will get the proper answer why it is giving you 500 error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900