Reputation: 63
I have an asp.net mvc 5 webapp, I want to get a DropDownListFor and add a condition to it.
In the next view I want to display in the DropDownList only Cars where Cars.ClientId==model.ClientId . So what can I add to the SelectListItem to get that?
I need something like this (which is not working):
Cars = db.Cars.Select(c => new SelectListItem() { Text = c.Licence, Value = c.Id.ToString() }).ToList().Where(item=>item.ClientId== id)
Here is the 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
});
});
This is the model 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>();
}
}
}
And here is the controller:
// 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);
}
Upvotes: 1
Views: 3629
Reputation: 3015
This is the wrong part
Cars = db.Cars
.Select(c => new SelectListItem()
{
Text = c.Licence,
Value = c.Id.ToString()
}).ToList()
.Where(item=>item.ClientId== id)
First get the filtered records from database then use it.
Cars = db.Cars
.Where(item=>item.ClientId== id)
.Select(c => new SelectListItem()
{
Text = c.Licence,
Value = c.Id.ToString()
});
Upvotes: 5