Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用mvc根据国家选择用户?_Javascript_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Javascript 如何使用mvc根据国家选择用户?

Javascript 如何使用mvc根据国家选择用户?,javascript,c#,asp.net,asp.net-mvc,asp.net-mvc-4,Javascript,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有两个表一个是国家表另一个是医生表都包含国家字段。如果我根据国家名称从国家中选择任何国家名称,我希望从医生表中显示医生名称 <div class="col-lg-4"> <fieldset class="form-group"> <label class="form-label" for="exampleInputEmail1">Count

我有两个表一个是国家表另一个是医生表都包含国家字段。如果我根据国家名称从国家中选择任何国家名称,我希望从医生表中显示医生名称

<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>
基于一个国家ID的医生绑定

区域医生绑定

    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]working fine

您正在传递字符串Company\u Bind作为ajax调用的url。因此,它将被附加到当前页面的url中,并对此进行调用

例如,如果当前页面是您的BaseURL/Home/Index,则会调用您的BaseURL/Home/Index/Company\u Bind?CountryID=4

如果您的当前页面是您的BaseURL/Doctors/List,则会调用您的BaseURL/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 helper生成Url并将其传递给外部js文件,如中所述。

问题是什么?看起来您正在进行ajax调用以获取数据医生姓名未启用@shyju您的意思是,您正在填充选择列表,但控件已禁用?是的,列出了正确的国家/地区值,但基于国家/地区id,医生姓名未显示@shyjuno display和not populated是两件不同的事情!您需要清楚地指定当前正在发生的事情以及您预期的行为。你的代码看起来不错。检查页面中是否有任何脚本错误
 //[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();
        }
$.get("@Url.Action("Company_Bind")", { CountryID: id }, function (data) {

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

});