0

I have two tables one is country table another one is doctor table both contains country fields.if i select any country name from country based on the country name i want to display doctors name from the doctor table.

<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>

Country bind Code:

#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

DAL :

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 based one country ID

#region Doctor Bind

    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>

Doctor Table

Country

If i use [Authorize] i cann't use ajax function

 //[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();
        }

if i don't use [Authorize] working fine

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • What is the issue ? Looks like you are making an ajax call to get the data – Shyju Nov 16 '17 at 13:15
  • Doctor Name is not enabled @Shyju – Ivin Raj Nov 16 '17 at 13:16
  • You mean, you are getting the SELECT list populated, but the control is DISABLED ? – Shyju Nov 16 '17 at 13:17
  • Yes correct country value is listed but based on country id doctor name is not displayed @Shyju – Ivin Raj Nov 16 '17 at 13:19
  • can you please also post the response you might be getting while making ajax call to get doctors. this will be available in the browsers console>Network tab (in case of chrome) – Parv Sharma Nov 16 '17 at 13:21
  • 1
    Not displayed and not populated are 2 different things ! You need to clearly specify what is happening right now and what is your expected behavior. Your code looks fine. Check whether you have any script errors in the page – Shyju Nov 16 '17 at 13:21
  • Country ID doesn't passed to the controller Company_Bind..@Shyju – Ivin Raj Nov 16 '17 at 13:32
  • Country ID doesn't passed to the controller Company_Bind.@ParvSharma – Ivin Raj Nov 16 '17 at 13:44
  • Failed to load resource: the server responded with a status of 404 (Not Found @Shyju jquery.min.js" file – Ivin Raj Nov 16 '17 at 14:03
  • That means your page does not have jQuery loaded. Fix that. You need to include jquery library to use `$` – Shyju Nov 16 '17 at 14:33
  • i solved that issues but i have one doubt above i added the code please check it and tell me what was my fault?@Shyju – Ivin Raj Nov 16 '17 at 14:39

1 Answers1

1

You are passing the string Company_Bind as the url for the ajax call. So it will be appended to the current pages url and a call will be made to that.

For example, if your currrent page is yourBaseUrl/Home/Index, the call will be made to yourBaseUrl/Home/index/Company_Bind?CountryID=4

If your current page is yourBaseUrl/Doctors/List, the call will be made to yourBaseUrl/Doctors/List/Company_Bind?CountryID=4

This will give you 404 response as that is invalid url to your action method.

You should consider using the Url.Action helper method to generate the correct relative path to the action method, irrespective of your current page.

So if your javascript code was inside a razor view,you can directly use it like

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

});

Or use the Url.Action overload which accepts the action method name and controller name.

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

});

If it is in an external js file, you can still use the Url.Action helper in the razor view to generate the url and pass it to the external js file as explained in this post.

Shyju
  • 214,206
  • 104
  • 411
  • 497