2013-02-25 47 views
1

查看ASP.NET MVC 3型空對提交

@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現在有公衆制定者。這個問題似乎仍然存在。請參閱本圖片:

enter image description here

+1

你有2'@ Html.BeginForm?'是故意的嗎? – codingbiz 2013-02-25 09:00:49

+0

刪除多餘的,還是空的提交。 – 2013-02-25 09:06:00

回答

3

是,你必須SurveyQuestions是一個私定的問題?

-old答案 -
你有2× (Html.BeginForm(Html.BeginForm 有一次,我忘了有一個using聲明,做了同樣的事情在我的形式。

+0

只是刪除1X BeginForm其仍然無效:( – 2013-02-25 09:05:27

+0

OK,會看你的代碼又... – 2013-02-25 09:06:06

+0

我已經更新了我的答案 – 2013-02-25 09:18:10

相關問題