c# - 超过 1 个 HttpPost 正在激活
问题描述
我正在制作一个页面,我希望能够根据正在执行的操作进行几种不同的操作。
例如,我有一个下拉列表,我希望根据下拉列表中的选择将不同的表单加载到页面中。
目前我正在这样做:
$(document).ready(function () {
$('#subject').on("change", function (e) {
e.preventDefault();
var selectedVal = $('#subject').val();
$.ajax({
url: "ContactUs/GetForm",
type: "POST",
data: { searchValue: selectedVal },
async: true,
success: function (data) {
$('#renderForms').empty();
$('#renderForms').append(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("An error has occured!!! " + xhr.status + " && " + xhr.responseText);
}
});
}; etc...
这命中下面的方法没问题
[HttpPost]
public ActionResult GetForm(string searchValue)
{
//my code here
return PartialView("_Bills",contactModel.contactBillsModel);
}
但是,当我单击它时,也会点击以下方法,这不是我的意图。
[HttpPost]
public ActionResult Index(Contact_Portal.Models.ContactViewModel contactModel)
{
return View(contactModel);
}
如何防止这种行为?
我在这样的部分页面的按钮中使用的最后一个 HttpPost
<input id="Send" type="submit" value="Send" class="btn btn-default" />
编辑: 根据请求添加附加信息。
带有下拉菜单的视图
@model WebPortal.Models.FormViewModel
@{
ViewBag.Title = "multiple forms";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>multiple forms</h2>
<label>My selection is... </label>
@Html.DropDownListFor(model => model.contactSelectListItems, new List<SelectListItem>
{
new SelectListItem() {Text = "option 1", Value="option 1"},
new SelectListItem() {Text = "option 2", Value="option 2"},
new SelectListItem() {Text = "option 3", Value="option 3"},
new SelectListItem() {Text = "option 4", Value="option 4"}
}, "--choose--", new { id = "subject", @class="dropdown-item"})
<div id="renderForms">
</div>
具有以下形式的视图之一:
@model WebPortal.Models.AformViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>A Form</h4>
<hr />
<div id="InfoBox" class="form-group col-md-20">
Remember to fill out this text with relevant text.
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container">
<div class="row">
<div class="form-group form-group-sm col-sm-6">
<div class="row">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-9">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group form-group-sm col-sm-6">
<div class="row">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-9">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group form-group-sm col-sm-6">
<div class="row">
<div class="col-sm-9">
<!--@Html.Action("Index", "ContactUs")-->
<input id="Send" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
</div>
</div>
</div>
解决方案
好吧,我有一个解决方案,您对此并不满意,但它确实有效。
这是我发布问题后所做的事情:我已根据 Stephen Muecke 的要求在此处更改了这一部分:
<div class="form-group form-group-sm col-sm-6">
<div class="row">
<div class="col-sm-9">
<!--@Html.Action("Index", "ContactUs")-->
<input id="Send" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
至
<div class="form-group form-group-sm col-sm-6">
<div class="row">
<div class="col-sm-9">
<input id="Send" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
之后它消除了我激活 HttpPost Actions GetForm 和 Index(viewmodel) 的问题
但我遇到了提交按钮激活 GetForm 操作而不是 Index(viewmodel) 操作的新问题。因此,我将提交按钮更改为以下
<input id="Send" type="submit" value="Send" formaction=@Url.Action("Index","ContactUs") class="btn btn-default" />
这个改变暂时解决了我的问题,但我觉得现在输入提交按钮现在不符合不显眼的事件处理背后的想法。
编辑:
而不是上面的提交按钮行,我将它添加到 html 表单中:
@using (Html.BeginForm("Index", "ContactUs"))
推荐阅读
- reactjs - 创建一个抽象逻辑的组件(从现有代码)
- android - 如何减少在 Android RecyclerVIew 中单击项目时的操作延迟?
- c++ - Vim 缩进 C++11 lambda,在新行上有左大括号
- javascript - JavaScript - 从“for”循环中选择最后一项
- python - 获取作为序列化对象发送的所请求用户的配置文件
- java - JSON解析时这段代码有什么问题?
- c# - 为什么在使用 fluent-nhibernate 配置 nhibernate 时出现 MissingMethodException?
- javascript - 无法使用 JavaScript 计算时差
- c++ - 擦除后重构列表迭代器
- java - 当我尝试使用 Hibernate 删除实体时,没有错误消息但不会删除