![](/img/trans.png)
[英]Send Dropdown Selected Value from View to Controller using "DropDownListFor" in MVC
[英]Unable to get value of DropDownListFor in controller on submit from view
我在我的頁面中使用@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>
看起來您正在將下拉列表映射到錯誤的成員:
@Html.DropDownListFor(m => m.Section.Type, new SelectList(Model.Type, "Value", "Text"))
Model.Type
是您的數據源(即List<SelectListItem>
),但您希望將所選值綁定到Model.Section.Type
,而不是Model.Type
。
另外,Khaled是對的,你確實有錯誤的值/文本參數,所以我在上面交換它們。 目前對您來說似乎不是問題的原因是因為您的文本和值是相同的。 只要你有任何不同,那就會給你一個問題。
我認為你不小心錯放了值和文本名稱:第二個參數應該是值,第三個是文本:
@Html.DropDownListFor(m => m.Type, new SelectList(Model.Type, "Value", "Text"))
編輯:試試
@Htm l.DropDownListFor(m => m.Section.SelectedType, new SelectList(Model.Type, "Value", "Text
”))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.