在asp.net mvc 5中更改日期后更新下拉列表

时间:2014-06-08 12:30:10

标签: jquery asp.net asp.net-mvc forms asp.net-mvc-5

在编写我的代码时,我偶然发现了这个问题:

我有以下代码:

 <div class="form-group">
        @Html.LabelFor(model => model.RegistryDate, new { @class = "control-label col-md-2" })
        <div class="col-sm-3">
            <div class='input-group date' id='datetimepicker1'>
                <input type='text' class="form-control" name="RegistryDate" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-nonescaped"></span>
                </span>
            </div>
        </div>
    </div>


    <div class="form-group">
        @Html.LabelFor(model => model.DoctorId, "Doctor", new { @class = "control-label col-md-2" })
        <div class="col-sm-3">
            @Html.DropDownDoctorsFor(model => model.DoctorId, new { @class = "col-sm-7 form-control"})
            @Html.ValidationMessageFor(model => model.DoctorId)
        </div>
    </div>

我的DropDownDoctor助手看起来像这样:

    public static MvcHtmlString DropDownDoctorsFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttrubutes, string name = "")
    {
        var htmlAttr = GenerateHtmlAttributes(htmlAttrubutes);

        var db = new MyContext();

        var doctors = db.Workers.OfType<Doctor>().ToList();

        var doctorsListItem = doctors.Select(doctor => new SelectListItem()
        {
            Text = doctor.FirstName + " " + doctor.LastName + " | " + WorkerHelper.GetNumberOfPatientsForDoctor(doctor.Id) ,
            Value = doctor.Id.ToString()
        });


        if (name == "")
            name = ExpressionHelper.GetExpressionText(expression);
        var value = expression.Compile()(helper.ViewData.Model).ToString();

        StringBuilder dropdown = new StringBuilder();
        dropdown.Append(String.Format(@"<select name='{0}' {1}>", name, htmlAttr));
        string selected = "";
        foreach (var item in doctorsListItem)
        {
            if (value == item.Value)
                selected = "selected='selected'";
            else selected = "";
            dropdown.Append(String.Format("<option value='{0}' {1} >{2}</option>", item.Value, selected, item.Text));
        }
        dropdown.Append(@"</select>");

        return MvcHtmlString.Create(dropdown.ToString());
    }

我想要做的是更新选择列表,以便在选定日期显示医生的患者数量,并使用今天的默认值。我完全不知道该怎么做。我会非常感谢任何帮助。

0 个答案:

没有答案