我在我的页面中使用@Html.DropDownListFor<>
绑定到我的ViewModel但是当我将页面提交给控制器进行处理时,我收到错误“需要输入类型”。这是一个错误抛出,因为我的VM“Type”的预言具有数据注释[Required]
。我认为这是因为@Html.DropDownList<>
使用name=Type
生成HTML代码,而生成的所有其他HTML都有name=Section.XXXX
,其中XXX是属性名称。假设我的理论是正确的,我一直试图弄清楚如何使@Html.DropDownList<>
生成name=Section.Type
属性。所以我的问题是,我的理论是否正确,如果是这样,如何让它生成正确的HTML,如果没有可能出错?
部分EF模式:
public class Section
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int16 ID { get; set; }
public Int64? LogoFileID { get; set; }
[Required, MaxLength(250), Column(TypeName = "varchar")]
public string RouteName { get; set; }
[Required, MaxLength(15), Column(TypeName = "varchar")]
public string Type { get; set; }
[Required]
public string Title { get; set; }
public string Synopsis { get; set; }
[ForeignKey("LogoFileID")]
public virtual File Logo { get; set; }
}
剖面视图模型:
public class SectionAddEditVM
{
public Section Section { get; set; }
public List<SelectListItem> Type { get; set; }
public string SelectedType { get; set; }
public SectionAddEditVM()
{
Section = new Section();
Type = new List<SelectListItem>();
Type.Add(new SelectListItem { Value = "Books", Text = "Books" });
Type.Add(new SelectListItem { Value = "Cinema", Text = "Cinema" });
Type.Add(new SelectListItem { Value = "Collection", Text = "Collection" });
Type.Add(new SelectListItem { Value = "Game", Text = "Game" });
}
}
查看:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken();
@Html.ValidationSummary(false)
<p>
<label for="Title">Seciton Title:</label> @Html.EditorFor(model => model.Section.Title)
<br />
<label for="RouteName">Section Route:</label> @Html.EditorFor(model => model.Section.RouteName)
<br />
<label for="Type">Section Type:</label> @Html.DropDownListFor(m => m.Type, new SelectList(Model.Type, "Text", "Value"))
<br />
<label for="LogoFile">Logo Image:</label> <input id="LogoFile" name="LogoFile" type="file" />
<br />
<label for="Synopsis">Synopsis:</label> @Html.EditorFor(model => model.Section.Synopsis)
<br />
<input type="submit" value="Add new section" />
</p>
}
控制器:
[Route("Add"), HttpPost, ValidateAntiForgeryToken]
public ActionResult AddSection(SectionAddEditVM NewSection, HttpPostedFileBase LogoFile)
{
if (ModelState.IsValid)
{
try
{
if (LogoFile != null && LogoFile.ContentLength > 0)
{
if (LogoFile.ContentType == "image/png")
{
string FileName = NewSection.Section.RouteName + "Logo";
NewSection.Section.LogoFileID = FileUploadHelper.UploadSiteImage("Logos", FileName, LogoFile);
}
else
{
ModelState.AddModelError("File Type", "Logo Files must be PNG files.");
return View(NewSection);
}
}
using (db)
{
db.Sections.Add(NewSection.Section);
db.SaveChanges();
}
//SiteUpdateHelper.RecordChange("New", "New Section", NewSection.Title, "Eagle_f90");
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
ModelState.AddModelError("Processing Error", "There was a problem processing the new section, please try again later.");
return View(NewSection);
}
}
return View(NewSection);
}
生成的HTML:
<select id="Type" name="Type"><option value="Books">Books</option>
<option value="Cinema">Cinema</option>
<option value="Collection">Collection</option>
<option value="Game">Game</option>
</select>
答案 0 :(得分:2)
看起来您正在将下拉列表映射到错误的成员:
@Html.DropDownListFor(m => m.Section.Type, new SelectList(Model.Type, "Value", "Text"))
Model.Type
是您的数据来源(即List<SelectListItem>
),但您希望将所选值绑定到Model.Section.Type
,而不是Model.Type
。
另外,Khaled是对的,你确实有错误的值/文字参数,所以我在上面交换它们。目前对您来说似乎不是问题的原因是因为您的文本和值是相同的。只要你有任何不同,那就会给你一个问题。
答案 1 :(得分:1)
我认为你不小心错放了值和文字名称: 第二个参数应该是值,第三个参数是文本:
@Html.DropDownListFor(m => m.Type, new SelectList(Model.Type, "Value", "Text"))
编辑: 试试
@Htm l.DropDownListFor(m => m.Section.SelectedType, new SelectList(Model.Type, "Value", "Text
“))