[英]Difficulties with DropDownlist in ASP.Net MVC5
I'm struggling with dropdownlist tried several methods online and all failed will show the methods that I tried. 我在dropdownlist上尝试了几种在线尝试的方法,所有尝试失败的方法都会显示我尝试过的方法。
Objective Create a Reciept with date,reference... & country. 目的创建包含日期,参考...和国家/地区的收据。 Country is Required and should be a dropdownlist.
国家/地区为必填项,应为下拉列表。
So the Table for Reciept ("ID, Name, Date, Address, City, CountryList). 所以表收据 (“ID,姓名,日期,地址,城市,CountryList)。
RecieptModel RecieptModel
public class TransactionModel
{
public int ID { get; set; }
public int Name{ get; set; }
public DateTime Date { get; set; }
public string Address { get; set;}
public string City { get; set; }
public CountryList CountryList { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<RecieptModel> Reciepts { get; set;}
public DbSet<CountryList> coutryList { get; set; }
}
CountryList CountryList
public class CountryList
{
public byte Id { get; set; }
public enum Country
{
Germany,
US,
UK
}
}
Controller 调节器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Date,City,CountryList")] Reciepts reciepts)
{
if (ModelState.IsValid)
{
db.Reciepts.Add(reciepts);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(reciepts);
}
View 视图
<div class="form-group">
@Html.LabelFor(model => model.CountryList, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.CountryList)
@Html.ValidationMessageFor(model => model.CountryList)
</div>
</div>
This failed I looked for several examples I'm trying to do it without the use of javascript. 失败了,我找了几个我不使用javascript的示例。 In the End I just want to learn how to implement a Dropdownlist & save it to the database allot of the methods that I tried to implement failed in MVC5.
最后,我只想学习如何实现Dropdownlist并将其保存到我尝试实现的MVC5中失败的方法的数据库分配中。
I would sugest you to add a html extension method that implement dropdownlist for a generic enumarator. 我建议您添加一个HTML扩展方法,该方法为通用枚举器实现dropdownlist。 Take a look at this answer .
看看这个答案 。
this is actually you write 这实际上是你写的
@Html.EnumDropDownListFor(model => model.CountryList)
Change is need to add name property in your country list (it must be ID that is your taken in your model) , so it must be like this to maintain country list value 需要进行更改以在国家列表中添加name属性(它必须是您在模型中采用的ID),因此必须这样才能保持国家列表的值
@Html.DropDownListFor(model => model.CountryList.Id, new SelectList(model => model.CountryList)
Becouse every HTML control need unique indentifier 因为每个HTML控件都需要唯一的标识符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.