【问题标题】:how to select User to Select based on country using mvc?如何使用 mvc 根据国家/地区选择要选择的用户?
【发布时间】: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


【解决方案1】:

您将字符串 Company_Bind 作为 ajax 调用的 url 传递。所以它会被附加到当前页面的 url 上,并且会调用它。

例如,如果您的当前页面是yourBaseUrl/Home/Index,则会调用yourBaseUrl/Home/index/Company_Bind?CountryID=4

如果您的当前页面是yourBaseUrl/Doctors/List,则呼叫将拨打至yourBaseUrl/Doctors/List/Company_Bind?CountryID=4

这将为您提供 404 响应,因为您的操作方法的 URL 无效。

您应该考虑使用Url.Action 辅助方法来生成操作方法的正确相对路径,而不管您当前的页面如何。

因此,如果您的 javascript 代码在 razor 视图中,您可以像这样直接使用它

$.get("@Url.Action("Company_Bind")", { CountryID: id }, function (data) {

});

或者使用接受动作方法名称和控制器名称的Url.Action 重载。

$.get("@Url.Action("Company_Bind","Doctors")", { CountryID: id }, function (data) {

});

如果它在外部 js 文件中,您仍然可以使用 razor 视图中的 Url.Action 帮助器生成 url 并将其传递给外部 js 文件,如this post 中所述。

【讨论】:

    最近更新 更多