2015-11-04 168 views
2

我需要使用jquery从下拉列表中选择值。然后我想将这个选定的值发送到我的控制器中的一个Action Result Method,可能使用Ajax调用。因为我很新,所以我不太确定我会如何实现这一点。 所以我有这样的一个操作方法在我的控制器......如何使用MVC,JQuery和Ajax将选定的值从下拉列表发送到控制器

public ActionResult GetVehiclePart(string id) 
{ 
    //i did like to send the selected value to this controller and filter data like below 
    var _partNumber = _catalogue.CatalogueData.FirstOrDefault(x => x.Partnumber == id && x.Veh_ID == selectedValue); 
    return PartialView("_Vehicle", _partNumber) 
} 

而且在我的剧本file`

//getting the value using jquery 
var vehicle = $("#ModelResult").val(); 
$.ajax({ 
    type: 'GET', 
    url: '../../VehiclesController/GetVehiclePart/' + vehicle, 
    dataType: 'json', 
    success: function(data) { 
     vehicle = $("#ModelResult").val(); 
     console.log(vehicle); // the value is printing here not sure how to post it to the controller 
    }, 
    async: false 
}); 

我可以在这里做一些错误,但如果有人将有助于对如何实现

+2

我建议不使用异步假 – guradio

+0

所以我应该把它设置为true?如果是这样的话......我需要做什么才能在控制器中接收选定的值? – 1future

+0

你知道你在做什么错,你冒险像一个外国人在一个新的国家没有地图 – madalinivascu

回答

2

您的网址不正确(除非您的控制器名称为VehiclesControllerController),dataType选项也是如此。改变你的AJAX调用

$.ajax({ 
    type: 'GET', 
    url: '@Url.Action("GetVehiclePart", "Vehicles")', // don't hard code your urls 
    data: { id: $("#ModelResult").val() }, // pass the value to the id parameter 
    dataType: 'html', // your returning a view, not json 
    success: function(data) { 
     // do something with the partial view you return? 
     $(someElement).html(data); 
    }); 
}); 

边注:在您的控制器方法的查询过滤根据双方的id价值和selectedValue数据。它不清楚selectedValue指的是什么。如果你想通过2个值到控制器,那么你可以使用

data: { id: $("#ModelResult").val(), selectedValue: anotherValue }, 

,改变你的方法

public ActionResult GetVehiclePart(string id, string selectedValue) 
+0

这使得很多的意义..但如果我想通过值作为第二个参数呢?他有可能吗? – 1future

+1

你见过编辑吗?它不清楚你实际想要通过什么样的价值,但你可以通过尽可能多的你想要的。 –

+0

这确实有助于解决问题。谢谢 – 1future

相关问题