0

空列表,我有一個視圖模型:提交成爲後+ MVC

public class RegistrationViewModel 
{ 
    public string Country { get; set; } 
    public ConfigurationParamValue CountryParam { get; set; } 
    public string Civility { get; set; } 
    public ConfigurationParamValue CivilityParam { get; set; } 
    [FirstNameValidator(Category = "Registration", IsLocal = false)] 
    public string FirstName { get; set; } 
    public ConfigurationParamValue FirstNameParam { get; set; } 
    [LastNameValidator(Category = "Registration", IsLocal = false)] 
    public string LastName { get; set; } 
    public List<int> Days { get; set; } 
    public int SelectedDay{ get; set; } 
    public List<Month> Months { get; set; } 
    public Month SelectedMonth { get; set; } 
    public List<int> Years { get; set; } 
    public int SelectedYear { get; set; } 
    public DateTime BirthDate { get; set; } 
} 

我創建這個視圖模型視圖:

@model Registration.Front.Web.Models.RegistrationViewModel 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>RegistrationViewModel</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Country) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Country) 
      @Html.ValidationMessageFor(model => model.Country) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Civility) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Civility) 
      @Html.ValidationMessageFor(model => model.Civility) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.FirstName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.FirstName) 
      @Html.ValidationMessageFor(model => model.FirstName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.LastName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.LastName) 
      @Html.ValidationMessageFor(model => model.LastName) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.BirthDate) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownListFor(model => model.SelectedDay, new SelectList(Model.Days)) 
      @Html.ValidationMessageFor(model => model.BirthDate) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Occupation) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Occupation) 
      @Html.ValidationMessageFor(model => model.Occupation) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ZipCode) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ZipCode) 
      @Html.ValidationMessageFor(model => model.ZipCode) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Email) 
      @Html.ValidationMessageFor(model => model.Email) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Password) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Password) 
      @Html.ValidationMessageFor(model => model.Password) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CGV) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CGV) 
      @Html.ValidationMessageFor(model => model.CGV) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Optin) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Optin) 
      @Html.ValidationMessageFor(model => model.Optin) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CNIL) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CNIL) 
      @Html.ValidationMessageFor(model => model.CNIL) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

這是我的位指示:

public ActionResult Index() 
     { 
     List<int> listDays = new List<int>(){1, 2, 3}; 
     return View(new RegistrationViewModel() { Days=listDays }); 
     } 

[HttpPost] 
public ActionResult Index(RegistrationViewModel rvm) 
     { 
     if (ModelState.IsValid) 
      { return RedirectToAction("Welcome"); } 

     return View(rvm); 
     } 

public ActionResult Welcome() 
     { 
     return View(); 
     } 

我的問題是在帖子中,屬性viewmodel的日子是空的!!!!!我怎樣才能糾正這一點?

+0

SelectedDay爲空? – Kyle

+0

哪一天屬性?你的意思是列表天? – ssilas777

+0

是提交中的rvm.day爲空 – user1428798

回答

0
在您查看

你沒有渲染的時間裏面形式

渲染裏面有NAME =「天」的形式隱藏字段,如

foreach(var day in @Model.Days) { <input type="hidden" name="Days" /> }

複製上面的代碼並將其粘貼後 <div class="editor-field"> @Html.EditorFor(model => model.CNIL) @Html.ValidationMessageFor(model => model.CNIL) </div>

現在,當您提交時,Days值也會提交給Post方法,您將在Days List中獲取值。

+0

好吧,那太好了。如果我們想要移除View中的列表元素,該怎麼辦? –