【发布时间】:2024-05-23 10:10:02
【问题描述】:
我有两张表,一张是国家/地区表,另一张是医生表,都包含国家/地区字段。如果我根据国家/地区名称从国家/地区选择任何国家/地区名称,我想从医生表中显示医生姓名。
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label" for="exampleInputEmail1">Country</label>
@Html.DropDownList("CountryID", null, "--- Select Country ---", new { @class = "select2-arrow" })
@Html.ValidationMessageFor(model => Model.CountryID, null, new { @style = "color: red" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label" for="exampleInput">Doctor Name</label>
<select id="UserRefID" class="select2-arrow"></select>
@Html.ValidationMessageFor(model => Model.UserRefID, null, new { @style = "color: red" })
</fieldset>
</div>
国家绑定代码:
#region Country
public void Country_Bind()
{
userType type = new userType();
DataSet ds = type.Get_Country();
List<SelectListItem> coutrylist = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
coutrylist.Add(new SelectListItem { Text = dr["CountryName"].ToString(), Value = dr["CountryID"].ToString() });
}
ViewBag.CountryID = coutrylist;
}
#endregion
达尔:
public DataSet Get_Country()
{
SqlCommand cmd = new SqlCommand("Select * From Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
Doctor Bind 基于一个国家/地区 ID
#region 医生绑定
public JsonResult Company_Bind(string CountryID)
{
userType type = new userType();
DataSet ds = type.Get_Doctor(CountryID);
List<SelectListItem> Doctorlist = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
Doctorlist.Add(new SelectListItem { Text = dr["Tittle"].ToString() + " " + dr["DoctorName"].ToString(), Value = dr["DoctorID"].ToString() });
}
return Json(Doctorlist, JsonRequestBehavior.AllowGet);
}
#endregion
public DataSet Get_Doctor(string CountryID)
{
SqlCommand com = new SqlCommand("Select * from DoctorRegistration where Country=@Country", con);
com.Parameters.AddWithValue("@Country", CountryID);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
<script>
$(document).ready(function () {
$("#CountryID").change(function () {
var id = $(this).val();
$("#UserRefID").empty();
$.get("Company_Bind", { CountryID: id }, function (data) {
var v = "<option>--- Select State ---</option>";
$.each(data, function (i, v1) {
v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";
});
$("#UserRefID").html(v);
});
});
});
</script>
如果我使用[Authorize] 我不能使用ajax 函数
//[Authorize]
//[InitializeSimpleMembership]
public class AccountController : Controller
{
//
// GET: /Account/
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
public ActionResult Register()
{
UserType_Bind();
Country_Bind();
//Doctor_Bind();
return View();
}
如果我不使用 [Authorize] 工作正常
【问题讨论】:
-
有什么问题?看起来您正在进行 ajax 调用以获取数据
-
医生姓名未启用@Shyju
-
您的意思是,您正在填充 SELECT 列表,但控件已禁用?
-
是的,列出了正确的国家/地区值,但未显示基于国家/地区 ID 的医生姓名@Shyju
-
不显示和不填充是两个不同的东西!您需要清楚地说明现在正在发生什么以及您的预期行为是什么。你的代码看起来不错。检查页面是否有脚本错误
标签: javascript c# asp.net asp.net-mvc asp.net-mvc-4