如何在asp.net mvc中使用jquery ajax在方法中调用字符串
Posted
技术标签:
【中文标题】如何在asp.net mvc中使用jquery ajax在方法中调用字符串【英文标题】:How to call a string in a method using jquery ajax in asp.net mvc 【发布时间】:2019-03-06 11:52:53 【问题描述】:我希望能够在 ajax 方法上使用“字符串”。
如果我使用“字符串”,如何在 SQL 语句中再次添加“where”
欲了解更多信息,请参阅此页面:https://www.aspsnippets.com/Articles/Cascading-Dependent-Country-State-City-DropDownLists-using-jQuery-AJAX-in-ASPNet-MVC.aspx
型号代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Mvc;
namespace tk3.Models
public class ViewModel
public ViewModel()
this.depart = new List<SelectListItem>();
this.department = new List<SelectListItem>();
this.gender = new List<SelectListItem>();
this.dryclean = new List<SelectListItem>();
public List<SelectListItem> depart get; set;
public List<SelectListItem> department get; set;
public List<SelectListItem> gender get; set;
public List<SelectListItem> dryclean get; set;
public int departId get; set;
public int typegenderId get; set;
public int genderId get; set;
public int btntagId get; set;
public int ServiceId get; set;
public int gendertypeId get; set;
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tk3.Models;
using System.Configuration;
using System.Data.SqlClient;
namespace tk3.Controllers
public class HomeController : Controller
// GET: Home
public ActionResult Index()
ViewModel model = new ViewModel();
model.depart = PopulateDropDown("SELECT * FROM depart", "servgend", "departId");
model.gender = PopulateDropDown("SELECT genderId, gender FROM gender", "gender", "genderId");
return View(model);
[HttpPost]
public JsonResult AjaxMethod(string type, int value)
ViewModel model = new ViewModel();
switch (type)
case "departId":
model.department = PopulateDropDown("SELECT btntagId, btntag FROM department WHERE departId = " + value, "btntag", "btntagId");
break;
case "btntagId":
model.dryclean = PopulateDropDown("SELECT typegenderId, type FROM dryclean WHERE btntagId = " + value, "type", "typegenderId");
break;
case "genderId":
model.dryclean = PopulateDropDown("SELECT genderId, type FROM dryclean WHERE typegenderId = " + value, "type", "genderId");
break;
case "typegenderId":
model.dryclean = PopulateDropDown("SELECT genderId, price FROM dryclean WHERE genderId = " + value, "price", "genderId");
break;
return Json(model);
[HttpPost]
public ActionResult Index(int departId, int btntagId, int typegenderId, int genderId, int gendertypeId, int ServiceId)
ViewModel model = new ViewModel();
model.depart = PopulateDropDown("SELECT departId, servgend FROM depart", "servgend", "departId");
model.gender = PopulateDropDown("SELECT genderId, gender FROM gender", "gender", "genderId");
model.department = PopulateDropDown("SELECT btntagId, btntag FROM department WHERE departId = " + departId, "btntag", "btntagId");
model.dryclean = PopulateDropDown("SELECT typegenderId, type FROM dryclean WHERE btntagId = " + btntagId, "type", "typegenderId");
model.dryclean = PopulateDropDown("SELECT genderId, type FROM dryclean WHERE typegenderId = " + typegenderId, "type", "genderId");
model.dryclean = PopulateDropDown("SELECT genderId, price FROM dryclean WHERE genderId = " + genderId, "price", "genderId");
return View(model);
private static List<SelectListItem> PopulateDropDown(string query, string textColumn, string valueColumn)
List<SelectListItem> items = new List<SelectListItem>();
string constr = ConfigurationManager.ConnectionStrings["tk"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
using (SqlCommand cmd = new SqlCommand(query))
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
while (sdr.Read())
items.Add(new SelectListItem()
Text = sdr[textColumn].ToString(),
Value = sdr[valueColumn].ToString()
);
con.Close();
return items;
CShtml:
@model tk3.Models.ViewModel
@
ViewBag.Title = "Index";
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Request</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
<br />
<br />
@Html.DropDownListFor(m => m.departId, Model.depart, "Please select department")
<br />
<br />
@Html.DropDownListFor(m => m.btntagId, Model.department, "Please select service")
<br />
<br />
@Html.DropDownListFor(m => m.genderId, Model.gender, "Please select gender")
<br />
<br />
@Html.DropDownListFor(m => m.typegenderId, Model.dryclean, "Please select item")
<br />
<br />
@Html.DropDownListFor(m => m.ServiceId, Model.dryclean, "Please select item")
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function ()
$("select").each(function ()
if ($(this).find("option").length <= 1)
$(this).attr("disabled", "disabled");
);
$("select").change(function ()
var value = 0;
if ($(this).val() != "")
value = $(this).val();
var id = $(this).attr("id");
$.ajax(
type: "POST",
url: "/Home/AjaxMethod",
data: 'type: "' + id + '", value: ' + value + '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
var dropDownId;
var list;
switch (id)
case "departId":
list = response.department;
DisableDropDown("#btntagId");
//DisableDropDown("#id");
PopulateDropDown("#btntagId", list);
break;
case "btntagId":
list = response.dryclean;
DisableDropDown("#typegenderId");
//DisableDropDown("#id");
PopulateDropDown("#typegenderId", list);
break;
case "genderId":
list = response.dryclean;
DisableDropDown("#typegenderId");
//DisableDropDown("#id");
PopulateDropDown("#typegenderId", list);
break;
case "typegenderId":
list = response.dryclean;
DisableDropDown("#ServiceId");
//DisableDropDown("#id");
PopulateDropDown("#ServiceId", list);
break;
,
failure: function (response)
alert(response.responseText);
,
error: function (response)
alert(response.responseText);
);
);
);
function DisableDropDown(dropDownId)
$(dropDownId).attr("disabled", "disabled");
$(dropDownId).empty().append('<option selected="selected" value="0">Please select</option>');
function PopulateDropDown(dropDownId, list)
if (list != null && list.length > 0)
$(dropDownId).removeAttr("disabled");
$.each(list, function ()
$(dropDownId).append($("<option></option>").val(this['Value']).html(this['Text']));
);
</script>
所以我想在我的控制器上实现下面这样的东西,并能够在 ajax 代码上执行它
示例:
[HttpPost]
public JsonResult AjaxMethod(string type, int value, string value2)
ViewModel model = new ViewModel();
switch (type)
case "departId":
model.department = PopulateDropDown("SELECT btntagId, btntag FROM department WHERE departId = " + value + "AND WHHERE services = " + value2, "btntag", "btntagId");
break;
【问题讨论】:
【参考方案1】:请尝试以下 ajax。
url: "/Home/AjaxMethod",
type:"POST",
data:JSON.stringify(type: id, value: value),
success: function (response)
【讨论】:
你的代码没有抛出任何错误。所以我应该不传递'value2'的参数,你必须记住'value'是一个整数。 是的,你可以传递 value2,即使它是一个整数。它将通过参数列表识别预期的功能。以上是关于如何在asp.net mvc中使用jquery ajax在方法中调用字符串的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.NET MVC 中使用 jQuery 渲染局部视图
如何在带有 ADO.NET 的 ASP.NET Core MVC 中使用 jQuery Ajax 自动完成
如何使用 jquery 或 ajax 在 c#/asp.net 中为 MVC 项目更新 razor 部分视图
ASP.NET MVC 主视图引用的js(jquery)在分部视图中无效,如何解决?