所以我有这个模型:
public class HoraireChefViewModel
{
public ChefModel mChef { get; set; }
public DateTime mDate;
public int mStartHour;
public int mEndHour;
public HoraireChefViewModel()
{
mDate = DateTime.Now;
}
}
当我在我的控制器上做一个get请求时,我这样做:
[HttpGet]
public ActionResult GérerHoraireChef(int? chefid)
{
ChefModel chefToShow = ChefManager.GetChefByID((int)chefid);
HoraireChefViewModel viewModel = new HoraireChefViewModel
{
mChef = chefToShow,
mEndHour = 0,
mStartHour = 0
};
ModelState.Remove("mEndHour");
ModelState.Remove("mStartHour");
return View(viewModel);
}
[HttpPost]
public ActionResult GérerHoraireChef(HoraireChefViewModel horaireViewModel, string submitButton)
{
(... some potato)
return View(horaireViewModel);
}
您可以看到我尝试解决此问题的许多尝试。
所以在我看来,我渲染这样的东西:
@model BoudhaJaune.ViewModels.HoraireChefViewModel
@{
ViewBag.Title = "Gérer l'horaire de " + Model.mChef.mName;
}
<h2>
Gérer l'horaire de @Model.mChef.mName
</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.HiddenFor(item => item.mChef.mChefID)
<div>
<div class="float-left">
@Html.ActionLink("Retourner à la liste", "GérerChef")
</div>
<div class="clear"></div>
</div>
<div style="background-color: white; border: 1px solid black; border-radius: 5px; padding: 5px; margin: 5px;">
<div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
<div class="float-left" style="width: 48%">
<div class="float-left" style="width: 30%">
Nom:
</div>
<div class="float-right" style="width: 70%">
@Html.TextBoxFor(item => item.mChef.mName, new { @style = "width: 300px;", @class = "disabled", disabled = "disabled" })
@Html.HiddenFor(item => item.mChef.mName)
</div>
</div>
<div class="float-left" style="width: 48%">
<div class="float-left" style="width: 30%">
Description:
</div>
<div class="float-right" style="width: 70%">
@Html.TextBoxFor(item => item.mChef.mDescription, new { @class = "disabled", disabled = "disabled", @style = "width: 300px" })
@Html.HiddenFor(item => item.mChef.mDescription)
</div>
</div>
<div class="clear"></div>
</div>
<div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
<div class="float-left" style="width: 33%">
<div class="float-left" style="width: 30%">
Date:
</div>
<div class="float-right" style="width: 70%">
@Html.TextBoxFor(item => item.mDate, Model.mDate.ToShortDateString(), new { @class = "datePicker", @title = "Choose a Date"})
</div>
</div>
<div class="float-left" style="width: 33%">
<div class="float-left" style="width: 30%">
Heure de début:
</div>
<div class="float-right" style="width: 70%">
@Html.TextBoxFor(item => item.mStartHour, new { @class = "positive-integer"})
</div>
</div>
<div class="float-left" style="width: 33%">
<div class="float-left" style="width: 30%">
Heure de fin:
</div>
<div class="float-right" style="width: 70%">
@Html.TextBoxFor(item => item.mEndHour, new { @class = "positive-integer"})
</div>
</div>
<div class="float-right">
<input type="submit" value="Ajouter une date" name="submitButton"/>
</div>
<div class="clear"></div>
</div>
@if (Model.mChef.mListMenuDisponibilités.Count > 0)
{
<div style="width: 100%; display: table; border-spacing: 5px; line-height: 40px">
</div>
}
</div>
}
但是我的主要问题是,由于某种原因,我在TexBoxFor
和mStartHour
属性的mEndHour
字段中输入的数据始终为0,即使我键入5或12。为什么,我该如何解决这个问题?
修改
这里是为两个有问题的字段生成的HTML代码:
<div class="float-left" style="width: 33%">
<div class="float-left" style="width: 30%">
Heure de début:
</div>
<div class="float-right" style="width: 70%">
<input class="positive-integer" data-val="true" data-val-number="The field Nullable`1 must be a number." id="mStartHour" name="mStartHour" type="text" value="0" />
</div>
</div>
<div class="float-left" style="width: 33%">
<div class="float-left" style="width: 30%">
Heure de fin:
</div>
<div class="float-right" style="width: 70%">
<input class="positive-integer" data-val="true" data-val-number="The field Nullable`1 must be a number." id="mEndHour" name="mEndHour" type="text" value="0" />
</div>
</div>
答案 0 :(得分:2)
您的属性需要有getter和setter才能让DefaultModelBinder
设置发布值的值。
public class HoraireChefViewModel
{
public ChefModel mChef { get; set; }
public DateTime mDate { get; set; } // change
public int mStartHour { get; set; } // change
public int mEndHour { get; set; } // change
public HoraireChefViewModel()
{
mDate = DateTime.Now;
}
}
附注:您不需要在GET方法中调用ModelState.Remove(..)
(此时没有添加到模型状态!)