gpt4 book ai didi

asp.net-mvc - MVC传统表单 Action 不调用 Controller 方法

转载 作者:行者123 更新时间:2023-12-02 17:43:35 26 4
gpt4 key购买 nike

我知道这个主题引起了许多开发人员的兴趣,并且在许多论坛上进行了讨论,但我一直无法找到完全解决我的问题的正确答案。也许我还没有努力寻找答案,在这种情况下,如果各位开发人员能让我知道任何有用的论坛,那就太好了。

我遇到的问题只是传统的 HTML 操作没有调用 Controller ActionResult 方法。

我有一个名为“_Form.cshtml”的部分 View ,如下所示:

<_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 =”submit“和”.Events(e => e .单击(“提交”))”提交按钮。

**出于某种原因,我没有使用 Html.BeginForm() 来调用 Controller 方法。所以我的选择是使用传统形式通过提交按钮将数据从 View 传递到 Controller 。

我也试过了..

<form action="myController/myAction" method="POST">

上面的格式但是我的ActionResult仍然没有被调用。

我认为我的主视图不会导致 ActionResult 不触发,因为它只有两个级联的 DropDownList,可以在客户端选择正确的表单。

有人可以指出我做错了什么或建议我使用替代方案吗?正如我上面提到的,我绝不会使用 Html.BeginForm() 只是因为我有那些似乎只适用于传统形式的 Telerik Kendo 验证..

非常感谢!

最佳答案

问题是您正在使用 JavaScript 事件处理表单的提交...

因此,如果您想保留提交按钮点击的 JS 事件,则需要使用 JS 方法中的 Ajax 调用来对操作执行 POST,如下所示:

$.ajax({
url: '/Product/Temp_Form',
data: $('#tempForm').serialize(),
type: 'POST',
});

关于asp.net-mvc - MVC传统表单 Action 不调用 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com