asp.net mvc 5具有条件的SelectListItem

时间:2015-05-19 08:51:37

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

我有一个asp.net mvc 5 webapp,我想获得一个DropDownListFor并为它添加一个条件。

在下一个视图中,我想在DropDownList中仅显示Cars.ClientId == model.ClientId的Cars。那么我可以将哪些内容添加到SelectListItem中呢?

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

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

这是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
});
});

这是模型CreateContractModel:

namespace BRMSWebApp.Models
{
public class CreateContractModel
{
    public int Id { get; set; }
    public DateTime? StartDate { get; set; }
    public float? AnnualPrice { get; set; }
    public Car Car { get; set; }
    public Client Client { get; set; }
    public ContractType ContractType { get; set; }

    public int? CarId { get; set; }
    public int? ContractTypeId { get; set; }

    public int? ClientId { get; set; }
    public List<SelectListItem> Cars { get;set; }
    public List<SelectListItem> ContractTypes { get; set; }

    public CreateContractModel()
    {
        this.Cars = new List<SelectListItem>();
        this.ContractTypes = new List<SelectListItem>();
    }
}
}

这是控制器:

 // GET: Contracts/Create
    public ActionResult Create(int id)
    {
        db.Cars.Select(c => new SelectListItem() { Text = c.Licence, Value = c.Id.ToString() }).ToList();
        var contractModel = new CreateContractModel()
        {
            ClientId = id,
            Cars = db.Cars.Select(c => new SelectListItem() { Text = c.Licence, Value = c.Id.ToString() }).ToList(),
            ContractTypes = db.ContractTypes.Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToList()
        };

        return View(contractModel);
    }

1 个答案:

答案 0 :(得分:5)

这是错误的部分

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