ASP.NET MVC表单提交索引而不是提交给正确的操作
我创建了要用作部分视图的表单。
I crated a form that is to be used as a partial view.
我在保存操作中放置了一个断点,并单击了提交按钮。 ,它将验证数据,但从未执行该操作,而是多次执行 Index
操作!
I placed a breakpoint at the 'Save' action, and when I click the submit button, it validates the data, but never reaches the action, instead the Index
action is reached several times!
此处代码是:
@model Models.Category
@using (Html.BeginForm("Save", "Categories", FormMethod.Post))
{
@Html.AntiForgeryToken()
<fieldset>
<legend>Category</legend>
@Html.HiddenFor(model => model.CategoryId)
<p>@((Model.CategoryId > 0 ? "Edit" : "New") + " category")</p>
<div class="editor-label">
@Html.LabelFor(model => Model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.Title)
@Html.ValidationMessageFor(model => Model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => Model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model.Description)
@Html.ValidationMessageFor(model => Model.Description)
</div>
<p>
<input type="submit" value="Save">
@Html.ValidationSummary(true)
</p>
</fieldset>
}
操作:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Category category)
{
throw new Exception("Exception has been thrown!");
}
这是我点击保存时发生的屏幕截图,验证错误出现,但没有调用 Save
操作,也没有引发异常。
而是 Index
操作已触发!
Here is a screenshot of what happens when I hit 'Save', validation error shows up, but the Save
action is not called, nor is the exception ever thrown.
Instead, the Index
action is triggered!
我还能检查什么以找出问题所在?谁正在将页面重定向到索引?
What else can I check to track down the issue? Who is redirecting the page to index???
您可以看到输出HTML 这里。
You can see the output HTML here.
我在 RouteConfig.cs中发现了问题
文件。
映射错误会混淆路由,我猜它使用了 Index
操作为默认操作,而不是由于设置错误而未找到的特定操作。
There was a wrong mapping that confused the routing and I guess it used the Index
action as a default instead of the specific action that wasn't found due to the wrong setting.