我有一个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; }
}
我用这个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>
这是我的控制者:
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();
}
我的问题是在帖子中,属性视图模型的天数为空!!!!!我怎么能纠正这个?
答案 0 :(得分:0)
在您的视图中,您不会在表格中呈现天数
在表单内渲染隐藏字段,其名称为“天”,如
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中获得值。