@model Survey.Models.TakeSurveyViewModel
@{
Layout = "~/Views/Shared/_SiteLayout.cshtml";
}
<h2>SURVEY : @Model.Title</h2>
<h3>@Model.Description</h3>
<hr />
@using (Html.BeginForm("SubmitSurvey", "HomePage", FormMethod.Post, new { id = "surveyForm" }))
{
for (int index = 0; index < Model.SurveyQuestions.Count; index++)
{
@* Editor Template - Null result *@
@*SurveyQuestionModel item = Model.SurveyQuestions[index];
@Html.EditorFor(x => item);*@
<p>@Model.SurveyQuestions[index].QuestionText</p>
@Html.DropDownListFor(item => Model.SurveyQuestions[index].OptionId, new SelectList(Model.SurveyQuestions[index].Options, "OptionId", "OptionText"), string.Empty)
}
<input type="submit" value="SubmitSurvey" />
}
視圖模型
public class TakeSurveyViewModel
{
public string Title { get; set; }
public string Description { get; set; }
public int SurveyId { get; set; }
public List<SurveyQuestionModel> SurveyQuestions { get; set; }
public TakeSurveyViewModel() { }
public TakeSurveyViewModel(int surveyId)
{
//Populate data - works ok.
}
}
DropDownList的型號
public class SurveyQuestionModel
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
[Required(ErrorMessage = "Please select an option.")]
public int OptionId { get; set; }
public IEnumerable<QuestionOption> Options { get; set; }
}
的頁面是雷德很好,所有的下拉菜單都有正確的選項。 ID和每名選擇也很獨特 -
控制器動作
[HttpPost]
public ActionResult SubmitSurvey(TakeSurveyViewModel model)
{
return !ModelState.IsValid ? TakeSurvey(model.SurveyId) : null;
}
但點擊提交按鈕,控制器動作模式爲空。
編輯:刪除了2倍HTML.BeginForm
編輯2:SurveyQuestions現在有公衆制定者。這個問題似乎仍然存在。請參閱本圖片:
你有2'@ Html.BeginForm?'是故意的嗎? – codingbiz 2013-02-25 09:00:49
刪除多餘的,還是空的提交。 – 2013-02-25 09:06:00