Well I want to pass two parameters using Json to the controller, where i use a query to get result....
好吧,我想使用Json将两个参数传递给控制器,在那里我使用查询来获得结果....
Now I manage to pass one parameter(first one) but the second just wont work, if i did Price/4 is will work but adding /2 says 404 in console...
现在我设法传递一个参数(第一个),但第二个只是不工作,如果我做Price / 4将工作但添加/ 2在控制台中说404 ...
//Passing ID 4 and ID 3 to HomeController/Price
$(document).ready(function () {
$("#type, #States").change(function () {
$.getJSON('/Home/Price/4/2', function (data) {
var items = '<option>Select a State</option>';
$.each(data, function (i, state) {
items += "<option value='" + state.Value + "'>" + state.Text +"</option>";
});
$("#Price").html(items);
});
});
});
//Home Controller Function
// Home Controller Function
public JsonResult States(int id, int ids)
{
using (TalyllynContext db = new TalyllynContext())
{
var query = from F in db.Fares
join p in db.TicketPreferences on
F.TicketPreferenceID equals p.ID
where F.TicketTypeID == id &&
F.TicketPreferenceID == ids
select p;
return Json(new SelectList(query.ToList(), "ID", "Name"),
JsonRequestBehavior.AllowGet);
}
}
Any more just let me know....
还有更多让我知道....
1 个解决方案
#1
1
You should define such behavior in the route or use the query parameters. I've improved JavaScript code with tips from Stephen Muecke.
您应该在路由中定义此类行为或使用查询参数。我使用Stephen Muecke的提示改进了JavaScript代码。
Query parameters approach:
查询参数方法:
$(document).ready(function () {
$("#type, #States").change(function () {
//It's better to use Url.Action helper to construct your urls,
//So that they always will be in sync with your routes
$.getJSON('@Url.Action("Price", "Home", new {id=4, ids=2})', function (data) {
var price = $("#Price");
price.append($('<option></option>').val('').text('Select a State'));
$.each(data, function (i, state) {
price.append($('<option></option>').val(state.value).text(state.text))
});
});
});
});
Or as stated by Stephen Muecke:
或者如Stephen Muecke所说:
$(document).ready(function () {
$("#type, #States").change(function () {
$.getJSON('@Url.Action("Price", "Home")', {id: 2, ids: 3}, function (data) {
var price = $("#Price");
price.append($('<option></option>').val('').text('Select a State'));
$.each(data, function (i, state) {
price.append($('<option></option>').val(state.value).text(state.text))
});
});
});
});
Route approach:
routed.MapRoute("Name", "{controller}/{action}/{id}/{ids}",
new {controller = "Home", action = "Index", ids=UrlParameter.Optional});
#1
1
You should define such behavior in the route or use the query parameters. I've improved JavaScript code with tips from Stephen Muecke.
您应该在路由中定义此类行为或使用查询参数。我使用Stephen Muecke的提示改进了JavaScript代码。
Query parameters approach:
查询参数方法:
$(document).ready(function () {
$("#type, #States").change(function () {
//It's better to use Url.Action helper to construct your urls,
//So that they always will be in sync with your routes
$.getJSON('@Url.Action("Price", "Home", new {id=4, ids=2})', function (data) {
var price = $("#Price");
price.append($('<option></option>').val('').text('Select a State'));
$.each(data, function (i, state) {
price.append($('<option></option>').val(state.value).text(state.text))
});
});
});
});
Or as stated by Stephen Muecke:
或者如Stephen Muecke所说:
$(document).ready(function () {
$("#type, #States").change(function () {
$.getJSON('@Url.Action("Price", "Home")', {id: 2, ids: 3}, function (data) {
var price = $("#Price");
price.append($('<option></option>').val('').text('Select a State'));
$.each(data, function (i, state) {
price.append($('<option></option>').val(state.value).text(state.text))
});
});
});
});
Route approach:
routed.MapRoute("Name", "{controller}/{action}/{id}/{ids}",
new {controller = "Home", action = "Index", ids=UrlParameter.Optional});