我的第一个 MVC 项目有问题。在选择Doct Down类型的DropdownList时,我正在尝试更改姓氏的DropdownList的值。我认为我的行动是有效的。但我不能在视图中使用结果。这是我的代码:
$(function () {
$('select#mCB').change(function () {
var docId = $(this).val();
$.ajax({
dataType: 'json',
data: 'spec=' + docId,
method: 'GET',
url: 'LoadDoctors',
success: function (data) {
$.each(data, function (key, Docs) {
$('select#shCB').append('<option value="0">Select One</option>');
$.each(Docs, function (index, docc) {
$('select#shCB').append(
'<option value="' + docc.Id + '">' + docc.Name + '</option>');
});
});
},
error: function (docID) {
alert(' Error !');
}
});
});
});Run Code Online (Sandbox Code Playgroud)
行动:
public static List<Docs> GetDoctorBySpec(string spec)
{
List<Docs> list = new List<Docs>();
string query = "select ID, Familiyasi, Speciality from Doktorlar where Speciality=@spec";
SqlConnection Connection = new SqlConnection(DataBase.ConnectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand(query, Connection);
cmd.Parameters.Add("@spec", spec);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(new Docs
{
Id = dr.GetString(0),
Name = dr.GetString(1)
});
}
return list;
}
enter code here
enter code here
[HttpGet]
public ActionResult LoadDoctors(string spec)
{
List<Docs> list = DoctorsService.GetDoctorBySpec(spec);
if (list == null)
{
return Json(null);
}
return Json(list);
}
Run Code Online (Sandbox Code Playgroud)
这里是我的 DropDownLists:
public static List<Docs> GetDoctorBySpec(string spec)
{
List<Docs> list = new List<Docs>();
string query = "select ID, Familiyasi, Speciality from Doktorlar where Speciality=@spec";
SqlConnection Connection = new SqlConnection(DataBase.ConnectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand(query, Connection);
cmd.Parameters.Add("@spec", spec);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(new Docs
{
Id = dr.GetString(0),
Name = dr.GetString(1)
});
}
return list;
}
enter code here
enter code here
[HttpGet]
public ActionResult LoadDoctors(string spec)
{
List<Docs> list = DoctorsService.GetDoctorBySpec(spec);
if (list == null)
{
return Json(null);
}
return Json(list);
}
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?感谢您的回答
小智 5
A500 (Internal Server Error)几乎总是意味着您在服务器上抛出异常。最好的猜测是在你的情况下,因为你的方法
DoctorsService.GetDoctorBySpec(spec);
Run Code Online (Sandbox Code Playgroud)
不接受null作为参数,而 的值spec是null因为您从未将其值传递给控制器。正如 stann1 已经注意到你的 ajax 选项需要是
data: {spec: docId},
Run Code Online (Sandbox Code Playgroud)
此外,您没有指定JsonRequestBehavior.AllowGet参数,这意味着该方法将失败。
所有这些都可以通过在服务器上和使用浏览器工具调试您的代码轻松确定(特别是Network查看正在发送和接收的内容以及错误消息的选项卡)
然而,这只是您的代码的众多问题之一。
首先,除非Docs只包含 2 个属性(选项的value属性和显示文本所需的值),否则您会向客户端发送大量从未使用过的数据,从而不必要地浪费带宽并降低性能。相反,发送一组匿名对象
[HttpGet]
public ActionResult LoadDoctors(string spec)
{
List<Docs> list = DoctorsService.GetDoctorBySpec(spec);
if (list == null)
{
return Json(null, JsonRequestBehavior.AllowGet);
}
var data = list.Select(d => new
{
Value = d.Id,
Text = d.Name
});
return Json(data, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
接下来,您的脚本将只生成多个<option value="0">Select One</option>元素(集合中的每个项目一个),因为datain$.each(data, function (key, Docs) {是您的集合,并且Docs是集合中的项目。你的第二个$.each()函数永远不会产生任何东西,因为Docs它不是一个集合。
您的脚本应该是(请注意,我使用了简短版本$.getJSON()而不是较长版本,$.ajax()并且还使用id了 html 助手生成的默认属性 - 不清楚您为什么要更改 id 的使用new { id = "mCB" }?)
var url = '@Url.Action("LoadDoctors")'; // never hard code your url's
var shifokori = $('#Shifokori'); // cache it
$('#DoktorTuri').change(function () {
$.getJSON(url, { spec: $(this).val() }, function(data) {
if (!data) { // only necessary depending on the version of jquery
// oops
}
// clear existing options and append empty option with NULL value (not zero)
// so validation will work
shifokori.empty().append($('<option></option>').val('').text('Select One'));
$.each(data, function (index, doc) {
shifokori.append($('<option></option>').val(doc.Value).text(doc.Text));
});
}).fail(function (result) {
// oops
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23537 次 |
| 最近记录: |