目前我做的很小"租一辆车" ASP.NET MVC中的应用程序。
我想为管理员创建一个页面,在那里他可以添加他想要提供给客户的汽车。在这个页面上,他可以添加汽车图片,生产年份,发动机类型等。此外,我想在此页面下拉列表中,他可以选择是否有空调。该下拉列表有两个选项"是"和"不"所以,如果他例如选择"是"然后我想填充财产" AirConditioning"我的模特。我坚持这一部分,我不知道该怎么做。
这是我的" Car"模型:
public class Car
{
[Key]
public int CarID { get; set; }
public string Model { get; set; }
[DisplayName("Year of production")]
public int YearOfProduction { get; set; }
public string Price { get; set; }
public virtual ICollection<FilePath> FilePaths { get; set; }
public string AlternateText { get; set; }
public string AirConditioning { get; set; }
}
这是我的#34;创建&#34; (汽车)查看:
<h2>Create</h2>
@using (Html.BeginForm("Create", "Cars", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Car</h4>
<hr/>
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<div class="form-group">
@Html.LabelFor(model => model.Model, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.Model, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.Model, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.YearOfProduction, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.YearOfProduction, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.YearOfProduction, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.Price, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.Label("Photo", new {@class = "control-label col-md-2"})
<div class="col-md-10">
<input type="file" id="Photo" name="upload"/>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" name="commandName" value="Create" class="btn btn-default"/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是我的#34;创建&#34;方法&#34;汽车&#34;控制器:
[Authorize(Roles = "Administrator")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CarID,Model,YearOfProduction,Price")] Car car, HttpPostedFileBase upload)
{
if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
var photo = new FilePath
{
FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), //uniqueness of the file name
FileType = FileType.Photo
};
car.FilePaths = new List<FilePath>();
upload.SaveAs(Path.Combine(Server.MapPath("~/Images/Cars"), photo.FileName));
car.FilePaths.Add(photo);
}
db.Cars.Add(car);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(car);
}
所以我想知道如何在现有代码中实现这个下拉列表。
答案 0 :(得分:0)
最简单的方法是构建一个名称与实体类属性名匹配的SELECT元素。
所以只需将其添加到您的视图(内部表单)
<SELECT name="AirConditioning">
<option value="Yes">Yes</option>
<option value="No">No</option>
</SELECT>
现在,当您提交表单时,所选的选项值将设置为AirConditioning
类对象的Car
属性。
如果您不想在视图中对选择选项进行硬编码,请继续阅读。
您需要将下拉列表的选项列表传递给视图。
以下示例显示使用ViewBag将选项传输到视图。
public ActionResult Create()
{
var options = new List<SelectListItem>{
new SelectListItem { Value ="Yes", Text="Yes"},
new SelectListItem { Value ="No", Text="No"}
};
ViewBag.AcOptions = options;
return View();
}
并且在您的视图中强烈键入Car
类,
@Html.DropDownListFor(x=>x.AirConditioning.ViewBag.AcOptions as List<SelectListItem>)
另一种解决方案是使用强类型来查看模型以传递下拉数据。 This post详细解释了这一点。
答案 1 :(得分:0)
尝试使用以下内容来定义视图中的下拉列表:
<div class="form-group">
@Html.LabelFor(model => model.AirConditioning, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.AirConditioning, new SelectList(new[] { "Yes", "No" }), htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AirConditioning, "", new { @class = "text-danger" })
</div>
</div>
这会将所选项目保存到模型中。