如何将更改的隐藏字段值从 ValidateForm JS 函数传递到 MVC 控制器
How do I pass the changed hidden field value from ValidateForm JS function to MVC controller
我的视图中有一个隐藏字段,我正在尝试在 validateForm() JavaScript 函数中为其添加一个值。我看到输出 window 上的值正在发生变化。但是,作为参数传递给控制器的模型接收为 Null。
这是我试过的代码
Cshtml:
@using (Html.BeginForm("ViewAch", "AchNew", FormMethod.Post, new { enctype = "multipart/form-data", id = "CreateACHForm", onsubmit = "validateForm()" }))
{
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
@Html.AntiForgeryToken();
@Html.HiddenFor(m => m.TokenizedPayloadNonce);
<input type="submit" value="SignUp" class="btn btn-primary btn-lg btn-block" />
}
JavaScript:
function validateForm() {
var NewToken = callExternalSytem(); // retrieving value from the external system
document.getElementById('TokenizedPayloadNonce').value = NewToken;
}
这是我从控制器读取它的方式:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewAch(AchProfile achProfile)
{
var token = achProfile.TokenizedPayloadNonce;
}
我可以访问其他字段值,但不能访问我在 validateForm() 中更新的字段值。有什么我想念的吗?
关于您的情况,您可以使用此方法将更新后的值发送到您的 Controller
:
我们首先将 SignUp
的类型从 submit
更改为 button
并将 validateForm
绑定到其 onclick
事件:
@using (Html.BeginForm("ViewAch", "AchNew", FormMethod.Post, new { enctype = "multipart/form-data", id = "CreateACHForm"}))
{
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
@Html.AntiForgeryToken();
<input type="hidden" id="myTokenizedPayloadNonce" name="TokenizedPayloadNonce"/>
<input type="button" value="SignUp" class="btn btn-primary btn-lg btn-block" onclick="validateForm()" />
}
绑定完成后,我们将使用 Javascript
将您的表单提交到 Controller
。这会将您需要的更新值绑定到 Model
:
function validateForm() {
var NewToken = callExternalSytem(); // retrieving value from the external system
document.getElementById('myTokenizedPayloadNonce').value = NewToken;
document.getElementById('CreateACHForm').submit();
}
我的视图中有一个隐藏字段,我正在尝试在 validateForm() JavaScript 函数中为其添加一个值。我看到输出 window 上的值正在发生变化。但是,作为参数传递给控制器的模型接收为 Null。
这是我试过的代码
Cshtml:
@using (Html.BeginForm("ViewAch", "AchNew", FormMethod.Post, new { enctype = "multipart/form-data", id = "CreateACHForm", onsubmit = "validateForm()" }))
{
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
@Html.AntiForgeryToken();
@Html.HiddenFor(m => m.TokenizedPayloadNonce);
<input type="submit" value="SignUp" class="btn btn-primary btn-lg btn-block" />
}
JavaScript:
function validateForm() {
var NewToken = callExternalSytem(); // retrieving value from the external system
document.getElementById('TokenizedPayloadNonce').value = NewToken;
}
这是我从控制器读取它的方式:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ViewAch(AchProfile achProfile)
{
var token = achProfile.TokenizedPayloadNonce;
}
我可以访问其他字段值,但不能访问我在 validateForm() 中更新的字段值。有什么我想念的吗?
关于您的情况,您可以使用此方法将更新后的值发送到您的 Controller
:
我们首先将 SignUp
的类型从 submit
更改为 button
并将 validateForm
绑定到其 onclick
事件:
@using (Html.BeginForm("ViewAch", "AchNew", FormMethod.Post, new { enctype = "multipart/form-data", id = "CreateACHForm"}))
{
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
<label>First Name <span>*</span></label>
@Html.TextBoxFor(m => m.FirstName, new { maxlength = 19, @class = "input-required form-control form-control-sm", id = "fname" })
@Html.ValidationMessageFor(m => m.FirstName, "")
@Html.AntiForgeryToken();
<input type="hidden" id="myTokenizedPayloadNonce" name="TokenizedPayloadNonce"/>
<input type="button" value="SignUp" class="btn btn-primary btn-lg btn-block" onclick="validateForm()" />
}
绑定完成后,我们将使用 Javascript
将您的表单提交到 Controller
。这会将您需要的更新值绑定到 Model
:
function validateForm() {
var NewToken = callExternalSytem(); // retrieving value from the external system
document.getElementById('myTokenizedPayloadNonce').value = NewToken;
document.getElementById('CreateACHForm').submit();
}