C# asp.net mvc 5带条件的SelectListItem

C# asp.net mvc 5带条件的SelectListItem,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我有一个asp.net mvc 5网络应用程序,我想获得一个DropDownList for并为其添加一个条件 在下一个视图中,我想在下拉列表中仅显示Cars.ClientId==model.ClientId的汽车。那么,我可以向SelectListItem添加什么来获得它呢 我需要这样的东西(不起作用): 以下是wiew: @model BRMSWebApp.Models.CreateContractModel @{ ViewBag.Title = "Ajouter"; } <

我有一个asp.net mvc 5网络应用程序,我想获得一个DropDownList for并为其添加一个条件

在下一个视图中,我想在下拉列表中仅显示Cars.ClientId==model.ClientId的汽车。那么,我可以向SelectListItem添加什么来获得它呢

我需要这样的东西(不起作用):

以下是wiew:

@model BRMSWebApp.Models.CreateContractModel 
@{
  ViewBag.Title = "Ajouter";   } 
<h2>Ajouter</h2>
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ClientId)
<div class="form-horizontal">
    <h4>Contrat</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.AnnualPrice, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.AnnualPrice, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AnnualPrice, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Car, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(c => c.CarId, Model.Cars)
            @Html.ValidationMessageFor(model => model.CarId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ContractType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(c => c.ContractTypeId, Model.ContractTypes)
            @Html.ValidationMessageFor(model => model.ContractTypeId, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Ajouter" class="btn btn-default" />
        </div>
    </div>
</div>
}
<div>
@Html.ActionLink("Retour à la liste", "Index")
</div>
<script type="text/javascript">
$(document).ready(function () {
    //$("#StartDate").datepicker($.datepicker.regional["fr"]);
    $("#StartDate").datepicker({
  changeMonth: true,
  changeYear: true
});
});
这是错误的部分

Cars = db.Cars
         .Select(c => new SelectListItem() 
                         { 
                           Text = c.Licence, 
                           Value = c.Id.ToString() 
                }).ToList()
         .Where(item=>item.ClientId== id)
首先从数据库中获取过滤后的记录,然后使用它

Cars = db.Cars
         .Where(item=>item.ClientId== id)
         .Select(c => new SelectListItem() 
                         { 
                           Text = c.Licence, 
                           Value = c.Id.ToString() 
                 });
这是错误的部分

Cars = db.Cars
         .Select(c => new SelectListItem() 
                         { 
                           Text = c.Licence, 
                           Value = c.Id.ToString() 
                }).ToList()
         .Where(item=>item.ClientId== id)
首先从数据库中获取过滤后的记录,然后使用它

Cars = db.Cars
         .Where(item=>item.ClientId== id)
         .Select(c => new SelectListItem() 
                         { 
                           Text = c.Licence, 
                           Value = c.Id.ToString() 
                 });

首先过滤数据库值,然后创建
IEnumerable
-
db.Cars.Where(c=>c.ClientId==id)。选择(c=>new SelectListItem(){..
首先过滤数据库值,然后创建
IEnumerable
-
db.Cars.Where(c=>c.ClientId==id)。选择(c=>new SelectListItem(){..
Cars = db.Cars
         .Where(item=>item.ClientId== id)
         .Select(c => new SelectListItem() 
                         { 
                           Text = c.Licence, 
                           Value = c.Id.ToString() 
                 });