0

J'ai un ViewModel:soumettre une liste deviendra nulle dans le poste + 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; } 
} 

Je crée une vue avec ce viewmodel:

@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> 

ceci est mon contoller:

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(); 
     } 

Mon problème est dans le post, la propriété Jours du viewmodel est nul !!!!! Comment puis-je corriger cela?

+0

SelectedDay est null? – Kyle

+0

Quelle est la propriété de jour? vous voulez dire Liste Jours? – ssilas777

+0

oui rvm.day dans le soumettre est nul – user1428798

Répondre

0

dans votre vue vous pas le rendu jours à l'intérieur sous forme

Render champs cachés à l'intérieur sous forme avec name = « Jours » comme

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

copier le code ci-dessus et collez-après <div class="editor-field"> @Html.EditorFor(model => model.CNIL) @Html.ValidationMessageFor(model => model.CNIL) </div>

maintenant, lorsque vous soumettez, les valeurs de jours seront également soumis à la méthode post et vous obtiendrez des valeurs dans la liste Jours.

+0

Ok c'est génial. Que faire si nous voulons supprimer et élément de liste dans View? –

Questions connexes