下拉列表(ASP.NET MVC)

时间:2017-03-15 22:50:33

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我的视图中有两个下拉列表

对于第一个下拉列表,我在View中有这个代码:

@Html.DropDownListFor(model => model.VacancyId, ViewBag.Teams as SelectList,new { @class = "greeting" })

这在Controller

SelectList teams = new SelectList(db.Vacancy, "VacancyId", "VacancyName");
        ViewBag.Teams = teams;
        return View();

这是模特

 public partial class Vacancy
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Vacancy()
    {
        this.Interwiers = new HashSet<Interwier>();
        this.InvitationMails = new HashSet<InvitationMail>();
        this.Interviews = new HashSet<Interview>();
        this.MassLinks = new HashSet<MassLink>();
    }

    public int VacancyId { get; set; }
    [Display(Name = "Вакансия")]
    public string VacancyName { get; set; }
    public Nullable<int> CompanyID { get; set; }

    public virtual Company Company { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Interwier> Interwiers { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<InvitationMail> InvitationMails { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Interview> Interviews { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<MassLink> MassLinks { get; set; }
}

}

我需要设置两个下拉列表。在我选择的公司中,第二,我将看到与该公司相关的职位空缺。怎么做?

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您可以使用AJAX。我不认为你可以通过Razor做到这一点。

答案 1 :(得分:0)

我将在MVC 5中给你一个例子。 提取增值税代码的增值税百分比,

打字稿/ JS:

     function FetchVATDetails() {

    let ddlSender = "#ddlVATCode";
    let url = "/ExactSalesOrders/GetVATDetails";
    let ddlVatPercentage = "#ddlVATPercentage";
    let failureFunction = () => {
        $(ddlVatPercentage).html("");
        $(ddlVatPercentage).append(
            $('<option></option>').html("- No VAT Percentage -"));
    };

    let id = $(ddlSender).val();
    if (id != "" && id != null) {
        $.ajax({
            url: url,
            type: "GET",
            dataType: "JSON",
            data: { id: id },
            success(data) {
                if (data === false) {
                    failureFunction();
                } else {
                    $(ddlVatPercentage).html("");
                    $.each(data.VATPercentages,
                        (i, percentage) => {
                            $(ddlVatPercentage).append(
                                $('<option></option>').val(percentage.Percentage).html(percentage.Percentage));
                        });
                    $(ddlVatPercentage).change();
                }
            }
        });
    } else {
        failureFunction();
    }
}

控制器:

 public ActionResult GetVATDetails(string id)
        {
            var vat = ExactService.SelectObjectByCode<VATCode>(id);
            vat.VATPercentages = ExactService.SelectObjects<VatPercentage>().Where(x => x.VATCodeID == vat.ID).ToList();
            return vat != null ? Json(vat, JsonRequestBehavior.AllowGet) : Json(false, JsonRequestBehavior.AllowGet);
        }

干杯

答案 2 :(得分:0)

你可以简单地通过使用J-query Ajax来实现它。如果你加载了第一个下拉值,为该字段添加一个OnChange方法,而在更改时放置一个Ajax调用并传递你的第一个下拉值并得到相关的值在db。确保在db。

中正确映射了字段值