选择asp.net MVC drobdownlist时如何显示相应的值 [英] How to display corresponding value when asp.net MVC drobdownlist selected

查看:69
本文介绍了选择asp.net MVC drobdownlist时如何显示相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表有productId,productName,Price。

我把这个表绑定在Dropdownlist中这样显示productName,

 < span class =code-keyword> public  ActionResult Index()
{
ViewBag.Products = new SelectList(storeDb。产品, ProductId < span class =code-string>名称);
return View();
}





我的问题是,当我在下拉列表中选择特定的productName时,价格应显示在文本框中。请帮助我

这是写回价的代码

 [HttpPost] 
public ActionResult GetProductDetails( int id)
{
var p = storeDb.Products.Where(c = > c.ProductId == id).ToList();
return Json(p);
}





这是我的ajax脚本

 script type =   text / javascript >  
$ .ready(function(){
$( #Products)。change(function(){
// $(#txtPrice)。empty( );
$ .ajax({
url:' @ Url.Action (GetProductDetails)'
类型:' post'
dataType:' JSON'
data:{id:$( #Products)。val()},
成功:function(data){

$(' #txtPrice')。文本(data.Price);
},
错误:function(){alert(' 发生了一些不好的事情'); }

});

});
});

< / script >



但这不起作用,请回答我...

解决方案

.ready(function(){


#Products)。change(function(){
//


( #txtPrice)空();

I have table having productId,productName,Price.
I binded this table to display productName in Dropdownlist like this,

public ActionResult Index()
        {
            ViewBag.Products = new SelectList(storeDb.Products, "ProductId", "Name");
            return View();
        }



my question is when i select a specific productName in dropdown list the price should be shown in the text box. Pls help me
This is the code written to retrive price

[HttpPost]
       public ActionResult GetProductDetails(int id)
       {
           var p = storeDb.Products.Where(c => c.ProductId == id).ToList();
           return Json(p);
       }



and this is my ajax script

script type="text/javascript">
    $.ready(function () {
        $("#Products").change(function () {
            //$("#txtPrice").empty();
            $.ajax({
                url:'@Url.Action("GetProductDetails")',
                type:'post',
                dataType:'JSON',
                data: { id: $("#Products").val() },
                success: function (data) {
                
                    $('#txtPrice').text(data.Price);
                },
                error: function () { alert('something bad happened'); }
                
            });
            
            });
        });
  
</script>


but this is not working , please answer me...

解决方案

.ready(function () {


("#Products").change(function () { //


("#txtPrice").empty();


这篇关于选择asp.net MVC drobdownlist时如何显示相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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