在下拉列表ASP.NET中的更改事件 [英] On change event in dropdownlist ASP.NET

查看:66
本文介绍了在下拉列表ASP.NET中的更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想在我的asp.net mvc中使用jquery的onchange事件显示折扣价格



但是获得500内部服务器错误。

 GET http:// localhost:9421 / Products / GetDis?discount = 1& productName = apple 500(内部服务器错误)





查看代码是



 @foreach(模型中的var项目){
< tr>
< td>
@ Html.DisplayFor(modelItem => item.ProductName)
< / td>
< td>
@ Html.DisplayFor(modelItem => item.ProductPrice,new {@id =prisValue})
< / td>

< td>

@ Html.DropDownList(状态,新列表< 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(编辑,编辑,新{id = item.Id})|
@ Html.ActionLink(详细信息,详细信息,新{id = item.Id})|
@ Html.ActionLink(删除,删除,新{id = item.Id})
< / td>
< / tr>
}

< / table>
@section scripts {
< script>

函数ChangeDis(o,proName,targetPrice){
//获取触发元素
// 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},
成功:函数(数据){
$(targetPrice).text(data);
}
});
}
< / script>
}





我的尝试:



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

return View(db.Products.ToList());
}
public JsonResult GetDis(字符串折扣,字符串productName,十进制tPrice)
{
客户customer = new Customers();

var products = db.Products.ToList();

十进制价格;

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);
}
返回Json(JsonRequestBehavior.AllowGet);
}





如何在视图上显示优惠价格

解决方案
( #prisValue)VAL();
// var targetPrice =


(element).closest('td')。prev('td');


。 ajax({
url:'@ Url.Action(GetDis)',
data:{discount:o.value,productName:proName,tPrice:targetPrice},
成功:函数(数据){


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

解决方案

("#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) {


这篇关于在下拉列表ASP.NET中的更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆