在我的 MVC 应用程序中,有一部分用户必须在发送之前查看项目。他必须选择要发送多少个。
这是我写下来的观点:
@model MyApp.Models.ObjInfo
@{
ViewBag.Title = "Dispatch Item";
}
<h2>Dispatch Item</h2>
<h3>
<label>
For: @Html.DisplayFor(model => model.m_OtherObj.m_Name)
</label>
</h3>
@using (Html.BeginForm())
{
<fieldset>
<legend>OBJ_INFO</legend>
<table>
<tr>
<th>Obj Number <br/>
Obj Color <br/>
Mana Cost <br/>
(...)
</th>
<th>
@Html.DisplayFor(model => model.m_OtherObj.m_Name)<br/>
@Html.DisplayFor(model => model.m_OtherObj.m_Color)<br/>
(...)
</th>
</tr>
</table>
<p>
<span style="font-size: 1.2em">
Number In Stock: @Html.DisplayFor(model => model.m_NbStock)<br/>
Number already reserved (Ebay Auctions): @Html.DisplayFor(model => model.m_QtyAlreadyReserved)<br/>
</span>
</p>
<p>
Number items to dispatch: <input type="number" min="0" max="99" name="numberSent" value="int" step="0"/>
</p>
<input type="submit" value="Save"/>
</fieldset>
}
<span style="color:red; font-size: 1.7em; font-style: italic;">@ViewData["ErrorMessage"]</span>
@using (Html.BeginForm()) {
<p>
@Html.ActionLink("Back to listing", "SearchIndex")
</p>
}
反正你懂这个意思。问题是,每次我加载应用程序时,进入这个页面,然后点击“保存”按钮,即使我的方法的签名是好的,该项目也是 NULL:
[HttpPost]
public ActionResult DispatchItem(ObjInfo itemToSend, int? numberSent)
{
if (ModelState.IsValid)
{
if (numberSent == null)
{
ViewData["ErrorMessage"] = "Please put a value in the 'Number items to dispatch' field. Thank you.";
return View(itemToSend);
}
if (numberSent > itemToSend.m_NbStock)
{
ViewData["ErrorMessage"] = m_TooManyItems;
return View(itemToSend);
}
}
return RedirectToAction("SearchIndex");
}
所以,正如我所提到的,每次我进入第一行时,对象都是空的。但是第一次加载页面时,也会加载对象并显示正确的信息。
有人可以帮帮我吗?谢谢!