0

I have a ViewModel:

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

I create a view with this 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>

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

My problem is in the post, the property Days of the viewmodel is null!!!!! How can i correct this?

4

1 回答 1

0

在您的视图中,您没有在表单中呈现 Days

使用 name="Days" 渲染表单内的隐藏字段,例如

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 中获取值。

于 2013-07-19T05:31:06.013 回答