MVC 传统表单动作不调用控制器方法
MVC traditional form action not calling controller method
我知道这个话题引起了许多开发人员的兴趣并且在许多论坛上都被讨论过,但我一直没能找到完全解决我的问题的正确答案。可能我还没有努力去寻求答案,在这种情况下,如果你们的开发人员可以让我知道任何有用的论坛,那就太好了。
我遇到的问题只是传统的 HTML 操作没有调用控制器 ActionResult 方法。
我有一个名为“_Form.cshtml”的局部视图,如下所示:
<_Form.cshtml>
<form action="@Url.Action("Temp_Form", "Product")" method="post" class="k-content" id="tempForm">
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<p class="lead">Please fill in the following form.</p>
<br />
<div class="form-horizontal">
<div class="form-group">
@Html.Label("Brand", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().DropDownList()
.Name("ddlBrand")
.OptionLabel("--- Select ---")
.HtmlAttributes(new { id = "brand", required = "required", data_required_msg = "Select Brand", @class = "form-control" })
.DataTextField("Name")
.DataValueField("Id")
.BindTo(Products.ProductBrandCollection.LoadAll())
)
<span class="k-invalid-msg" data-for="ddlBrand"></span>
</div>
</div>
<div class="form-group">
@Html.Label("Range", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().TextBox()
.Name("tbRange")
.HtmlAttributes(new { required = "required", validationmessage = "Enter Range" })
)
<span class="k-invalid-msg" data-for="tbRange"></span>
</div>
</div>
</div>
<div id="buttondiv">
<div class="column">
<div class="vis" id="submitbutton">
@(Html.Kendo().Button()
.Name("btnSubmit")
.HtmlAttributes(new { type = "submit", @class = "button-next" })
.Icon("arrowhead-e")
.Content("SUBMIT")
.Events(e => e.Click("submit"))
)
</div>
</div>
</div>
</fieldset>
</form>
在主视图 "Product.cshtml" 中,我有以下 javascript 提交按钮的点击事件:
function submit(e) {
// handles hiding and showing divs
}
这是我的 ActionResult "Temp_Form",它位于 "ProductController.cs":
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Temp_Form(string ddlBrand, string tbRange)
{
TempData["brand"] = ddlBrand;
TempData["range"] = tbRange;
return Content("");
}
我怀疑它是“@Url.Action("ActionMethod", "Controller")” <- 这部分格式不正确或两者都 "type=" 提交”和“.Events(e => e.Click("submit"))”作为提交按钮。
**出于某种原因,我没有使用 Html.BeginForm() 来调用控制器方法。所以我的选择是使用传统形式通过提交按钮将数据从视图传递到控制器。
我也试过了..
<form action="myController/myAction" method="POST">
上面的格式但是我的ActionResult还是没有被调用
我认为我的主视图不会导致 ActionResult 不触发,因为它只有两个级联的 DropDownLists,可以在客户端选择正确的表单。
有人可以找出我做错了什么或给我建议替代方案吗?正如我上面提到的,我绝不会使用 Html.BeginForm() 只是因为我有那些似乎只适用于传统形式的 Telerik Kendo 验证..
非常感谢!
问题是您正在使用 JavaScript 事件处理表单提交...
因此,如果您想为提交按钮保留 JS 事件,则需要使用 Ajax 从您的 JS 方法调用来执行 POST 操作,例如:
$.ajax({
url: '/Product/Temp_Form',
data: $('#tempForm').serialize(),
type: 'POST',
});
您使用的是 Razor 语法,所以不要直接使用表单标签,请使用 Html.BeginForm()
,如下所示。
@using(Html.BeginForm("Temp_Form", "Product", FormMethod.Post, new {@class = "k-content", @id="tempForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
//your HTML here
</fieldset>
}
此外,如果可能,请使用强类型模型而不是松散的字符串属性,如 ddlBrand 和 tbRange。
我知道这个话题引起了许多开发人员的兴趣并且在许多论坛上都被讨论过,但我一直没能找到完全解决我的问题的正确答案。可能我还没有努力去寻求答案,在这种情况下,如果你们的开发人员可以让我知道任何有用的论坛,那就太好了。
我遇到的问题只是传统的 HTML 操作没有调用控制器 ActionResult 方法。
我有一个名为“_Form.cshtml”的局部视图,如下所示:
<_Form.cshtml>
<form action="@Url.Action("Temp_Form", "Product")" method="post" class="k-content" id="tempForm">
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<p class="lead">Please fill in the following form.</p>
<br />
<div class="form-horizontal">
<div class="form-group">
@Html.Label("Brand", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().DropDownList()
.Name("ddlBrand")
.OptionLabel("--- Select ---")
.HtmlAttributes(new { id = "brand", required = "required", data_required_msg = "Select Brand", @class = "form-control" })
.DataTextField("Name")
.DataValueField("Id")
.BindTo(Products.ProductBrandCollection.LoadAll())
)
<span class="k-invalid-msg" data-for="ddlBrand"></span>
</div>
</div>
<div class="form-group">
@Html.Label("Range", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().TextBox()
.Name("tbRange")
.HtmlAttributes(new { required = "required", validationmessage = "Enter Range" })
)
<span class="k-invalid-msg" data-for="tbRange"></span>
</div>
</div>
</div>
<div id="buttondiv">
<div class="column">
<div class="vis" id="submitbutton">
@(Html.Kendo().Button()
.Name("btnSubmit")
.HtmlAttributes(new { type = "submit", @class = "button-next" })
.Icon("arrowhead-e")
.Content("SUBMIT")
.Events(e => e.Click("submit"))
)
</div>
</div>
</div>
</fieldset>
</form>
在主视图 "Product.cshtml" 中,我有以下 javascript 提交按钮的点击事件:
function submit(e) {
// handles hiding and showing divs
}
这是我的 ActionResult "Temp_Form",它位于 "ProductController.cs":
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Temp_Form(string ddlBrand, string tbRange)
{
TempData["brand"] = ddlBrand;
TempData["range"] = tbRange;
return Content("");
}
我怀疑它是“@Url.Action("ActionMethod", "Controller")” <- 这部分格式不正确或两者都 "type=" 提交”和“.Events(e => e.Click("submit"))”作为提交按钮。
**出于某种原因,我没有使用 Html.BeginForm() 来调用控制器方法。所以我的选择是使用传统形式通过提交按钮将数据从视图传递到控制器。
我也试过了..
<form action="myController/myAction" method="POST">
上面的格式但是我的ActionResult还是没有被调用
我认为我的主视图不会导致 ActionResult 不触发,因为它只有两个级联的 DropDownLists,可以在客户端选择正确的表单。
有人可以找出我做错了什么或给我建议替代方案吗?正如我上面提到的,我绝不会使用 Html.BeginForm() 只是因为我有那些似乎只适用于传统形式的 Telerik Kendo 验证..
非常感谢!
问题是您正在使用 JavaScript 事件处理表单提交...
因此,如果您想为提交按钮保留 JS 事件,则需要使用 Ajax 从您的 JS 方法调用来执行 POST 操作,例如:
$.ajax({
url: '/Product/Temp_Form',
data: $('#tempForm').serialize(),
type: 'POST',
});
您使用的是 Razor 语法,所以不要直接使用表单标签,请使用 Html.BeginForm()
,如下所示。
@using(Html.BeginForm("Temp_Form", "Product", FormMethod.Post, new {@class = "k-content", @id="tempForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
//your HTML here
</fieldset>
}
此外,如果可能,请使用强类型模型而不是松散的字符串属性,如 ddlBrand 和 tbRange。