模型绑定上的 TextBoxFor 未发布修改后的值
TextBoxFor on model binding not posting modified value
所以我有这个模型:
public class HoraireChefViewModel
{
public ChefModel mChef { get; set; }
public DateTime mDate;
public int mStartHour;
public int mEndHour;
public HoraireChefViewModel()
{
mDate = DateTime.Now;
}
}
当我在控制器上发出获取请求时,我会这样做:
[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>
}
但是我的主要问题是,由于未知原因,我在 mStartHour
和 mEndHour
属性 的 TexBoxFor
字段中输入的数据始终为 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>
您的属性需要有 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(..)
(此时尚未向模型状态添加任何内容!)
所以我有这个模型:
public class HoraireChefViewModel
{
public ChefModel mChef { get; set; }
public DateTime mDate;
public int mStartHour;
public int mEndHour;
public HoraireChefViewModel()
{
mDate = DateTime.Now;
}
}
当我在控制器上发出获取请求时,我会这样做:
[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>
}
但是我的主要问题是,由于未知原因,我在 mStartHour
和 mEndHour
属性 的 TexBoxFor
字段中输入的数据始终为 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>
您的属性需要有 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(..)
(此时尚未向模型状态添加任何内容!)