Asp.net mvc 4 如何在MVC中通过$.getJson传递2个参数?
我正在用MVC做我的应用程序。我在列表中做了级联下拉列表。从下拉列表中选择值时,我通过$.getJson()将值传递给控制器。我正在将一个id传递给控制器。我的问题是我需要将2个id传递给控制器。是否可以向控制器发送2个ID 我的查看页面是Asp.net mvc 4 如何在MVC中通过$.getJson传递2个参数?,asp.net-mvc-4,Asp.net Mvc 4,我正在用MVC做我的应用程序。我在列表中做了级联下拉列表。从下拉列表中选择值时,我通过$.getJson()将值传递给控制器。我正在将一个id传递给控制器。我的问题是我需要将2个id传递给控制器。是否可以向控制器发送2个ID 我的查看页面是 <div> @Html.DropDownList("Business", ViewBag.CategoryID as SelectList ,"Select the Business Category", new
<div>
@Html.DropDownList("Business", ViewBag.CategoryID as SelectList ,"Select the Business Category", new { id = "Business" })
</div>
<div>
<label>Select the Service Type</label>
@*<select id="Buname" name="buname" style="width:150px"></select>*@
@Html.DropDownList("Service", ViewBag.Service as SelectList,"Select", new { id = "Service", name ="buname"})
</div>
<div>
<label> Select the Location</label>
<select id="Location" name="location" style="width:150px"></select>
</div>
<div>
<label> Select the Location</label>
<select id="Employee" name="employee" style="width:150px"></select>
</div>
<script type="text/javascript">
$(function () {
$('#Business').change(function () {
$.getJSON('/Appt/Categories/' + $('#Business').val(), function (data) {
var items = '<option> Any Hospital</option>'
$.each(data, function (i, Service) {
debugger;
items += "<option value='" + Service.Value + "'>" + Service.Text +
"</option>";
});
$("#Service").html(items);
});
});
$('#Service').change(function () {
$.getJSON('/Appt/Location/' + $('#Service').val(), function (data) {
var items = '<option> Any Location </option>';
$.each(data, function (i, location) {
items += "<option value='" + location.Value + "'>" + location.Text +
"</option>";
});
$("#Location").html(items);
});
});
$('#Location').change(function () {
$.getJSON('/Appt/Employee/' + $('#Location').val(), function (data) {
var items = '<option> Any Employee </option>';
$.each(data, function (i, employee) {
items += "<option value='" + employee.Value + "'>" + employee.Text +
"</option>";
});
$("#Employee").html(items);
$("Service").html(items);
});
});
});
</script>
在publicjsonresult Employee(intid)中,我需要传递(“#Service”)值。
有可能吗?有人能帮我吗?您可以使用
var url='@url.Action(“类别”、“应用”);//不要硬编码你的网址!
$.getJSON(url,{id:$('#Business').val(),otherProperty:anotherValue},函数(数据){
您可以使用
var url='@url.Action(“Categories”,“Appt”);//不要硬编码url!
$.getJSON(url,{id:$('#Business').val(),otherProperty:anotherValue},函数(数据){
是的,您只需向控制器添加另一个参数
操作
,如下所示
public JsonResult Categories(int id, int id2)
{
SYTEntities db = new SYTEntities();
var business = from s in db.tblBusinessCategories
where s.CategoryId == id
select s;
return Json(new SelectList(business.ToArray(),"BusinessID", "BusinessName"), JsonRequestBehavior.AllowGet);
}
以及您的视图中的以下更改
$.getJSON('/Appt/Categories/' + $('#Business').val(), { id2: ('#otherId2').val() }, function (data) {
var items = '<option> Any Hospital</option>'
$.each(data, function (i, Service) {
debugger;
items += "<option value='" + Service.Value + "'>" + Service.Text +
"</option>";
});
$("#Service").html(items);
});
$.getJSON('/Appt/Categories/'+$('#Business').val(),{id2:('#otherId2').val()},函数(数据){
var项目='任何医院'
$。每个(数据、功能(i、服务){
调试器;
项目+=“”+服务文本+
"";
});
$(“#服务”).html(项目);
});
是的,您只需向控制器添加另一个参数
操作
,如下所示
public JsonResult Categories(int id, int id2)
{
SYTEntities db = new SYTEntities();
var business = from s in db.tblBusinessCategories
where s.CategoryId == id
select s;
return Json(new SelectList(business.ToArray(),"BusinessID", "BusinessName"), JsonRequestBehavior.AllowGet);
}
以及您的视图中的以下更改
$.getJSON('/Appt/Categories/' + $('#Business').val(), { id2: ('#otherId2').val() }, function (data) {
var items = '<option> Any Hospital</option>'
$.each(data, function (i, Service) {
debugger;
items += "<option value='" + Service.Value + "'>" + Service.Text +
"</option>";
});
$("#Service").html(items);
});
$.getJSON('/Appt/Categories/'+$('#Business').val(),{id2:('#otherId2').val()},函数(数据){
var项目='任何医院'
$。每个(数据、功能(i、服务){
调试器;
项目+=“”+服务文本+
"";
});
$(“#服务”).html(项目);
});
当然可以,但是您的方法只接受一个参数。您想要传递的两个参数是什么?在(“#”位置)。change()函数中,我需要传递(“#位置”)和(“#服务”)ValueRefer回答,但您尚未显示该方法的签名应该是什么。此外,不要将SelectList
返回给客户端-您只返回从未使用过的额外数据。而是返回匿名对象的集合。当然,但您的方法只接受一个参数。您需要哪两个参数要传递?In(“#”Location).change()函数,我需要传递(“#Location”)和(“#Service”)ValueRefer回答,但您没有显示该方法的签名应该是什么。此外,不要将SelectList
返回给客户端-您只是返回了从未使用过的额外数据。而是返回匿名对象的集合。感谢您的帮助,我现在更改了代码。结果是:)谢谢你的帮助,我现在更改了代码。结果是:)