我正在构建一个 MVC 应用程序,我想提交一个表单。所以我做了这样简单的事情。这是“DisplayItems”视图:
@model List<MyApp.Models.Inventory>
@{
ViewBag.Title = "Display Items";
}
@using (Html.BeginForm())
{
<table>
<tr>
<th>Object Name</th>
<th>Number In Stock</th>
<th>Quantity To Send</th>
<th>Reserved for First Template</th>
<th>Reserved for Second Template</th>
<th>Reserved for Third Template</th>
<th>Number so far</th>
<th>Input quantity</th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.DisplayFor(_x => _x[i].m_Obj.m_ObjName)</td>
<td>@Html.DisplayFor(_x => _x[i].m_QtyToSendShow)</td>
<td>@Html.DisplayFor(_x => _x[i].m_NbInStock)</td>
<td>@Html.DisplayFor(_x => _x[i].m_QtyFirstTemplate)</td>
<td>@Html.DisplayFor(_x => _x[i].m_QtySecondTemplate)</td>
<td>@Html.DisplayFor(_x => _x[i].m_QtyThirdTemplate)</td>
<td>@Html.DisplayFor(_x => _x[i].m_QtyHold)</td>
<td>@Html.TextBoxFor(_x=>_x[i].m_QtyToSend)</td>
</tr>
}
</table>
<input type="submit" name="_submitButton" value="Confirm"/>
}
没什么特别的。但是,当用户单击“确认”按钮时,应用程序会不断返回到 PREVIOUS 视图,该视图是用户指定搜索的过滤引擎。
这是名为“SendItems”的先前视图:
@{
ViewBag.Title = "Send items";
}
<h2>Send Items</h2>
<p>
@using (Html.BeginForm())
{
Html.RenderAction("AdvancedSearch", "PartialViews");
}
@Html.ActionLink("Back to Selection", "MenuSelection")
</p>
部分视图是绑定到具有许多字段和输入按钮的模型的视图。单击按钮输入时,控制器方法如下所示:
public ActionResult SendItems(SearchEngineObject _searchObj, string _submitButton)
{
if (_submitButton == "Search")
{
bool isValid = ValidateSearchFields(_searchObj);
if (!isValid)
{
ViewData["ErrorMessage"] = m_MessageError;
return View();
}
m_ListToManage = m_InventoryManager.ListAvailableInventoryItems(_searchObj);
if (m_ListInventoryToManage.Count == 0)
{
ViewData["ErrorMessage"] =
"There are no inventory items belonging to the parameters you selected; " +
"please change your values and try again.";
return View();
}
return View("DisplayItems", m_ListInventoryToManage);
}
return View();
}
因此,当点击 SendItems 视图的输入按钮时,控制器会验证字段,然后根据过滤器检索库存项目列表并将其发送到“DisplayItems”视图。
但是刚进入视图,如果我单击“确认”按钮,它会直接将我发送回“SendItems”视图,而我希望它转到“DisplayItems”控制器方法。为什么?谁能解释我做错了什么?
编辑
我做了一个快速调试会话,我可以确认调试将我发送回“发送项目”控制器方法,而不是点击“显示项目”方法。