[英]ASP.NET MVC Core TextBoxFor event pass model from View to Controller
To be up front, my career has mostly been back end C# work and nothing on the front end, so my knowledge on all things front end is very limited.首先,我的职业生涯主要是在后端工作,前端什么都没有,所以我对前端的所有知识都非常有限。
To begin, I have scoured everywhere looking for how to pass a model from the view to the controller when the enter key is pressed while in a textbox.首先,我到处寻找如何在文本框中按下回车键时将 model 从视图传递到 controller。 I have looked at ajax and js, but no example showed me how to pass in a model.我查看了 ajax 和 js,但没有示例向我展示如何传入 model。 I got close and what resulted is the model was null while in the controller action method.我走近了,结果是 model 是 null 而在 controller 操作方法中。 As an example, the below code I would like to have an event on the Zip textbox when someone presses the Enter key while focus is in said textbox, it will pass the Model containing any and all values to the GotZip controller action.例如,下面的代码我想在 Zip 文本框上有一个事件,当有人在焦点位于所述文本框中时按下 Enter 键时,它会将包含任何和所有值的 Model 传递给 GotZip Z594C103F2C6E04EC3D18Z 操作。
Any assistance would be greatly appreciated!任何帮助将不胜感激!
Help me Obi-Wan Kenobi, you're my only hope...帮帮我欧比旺克诺比,你是我唯一的希望……
Index.cshtml索引.cshtml
@model MyProject.Models.MyModel
@{
ViewBag.Title = "MyApp";
}
<div class="text-left">
<form asp-controller="Home" asp-action="Index">
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
@Html.TextBoxForFor(model => model.Name)
</th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Address1)
@Html.TextBoxForFor(model => model.Address1)
</th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.City)
@Html.TextBoxForFor(model => model.City)
</th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.State)
@Html.TextBoxForFor(model => model.State)
</th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Zip)
@Html.TextBoxForFor(model => model.Zip)
</th>
</tr>
</table>
<input name="Submit" type="submit" asp-action="SubmitButton" value="Submit" />
</form>
</div>
MyModel.cs我的模型.cs
namespace MyProject.Models
{
public class MyModel
{
public string Name { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
}
HomeController.cs家庭控制器.cs
namespace MyProject.Controllers
{
[Authorize]
public class HomeController : Controller
{
public IActionResult SubmitButton([FromForm] MyModel model)
{
//do stuff with model
ModelState.Clear();
return View("Index", myModel);
}
public IActionResult GotZip([FromForm] MyModel model)
{
//do stuff with model
ModelState.Clear();
return View("Index", myModel);
}
}
}
how to pass a model from the view to the controller when the enter key is pressed while in a textbox.当在文本框中按下回车键时,如何将 model 从视图传递到 controller。
I would like to have an event on the Zip textbox when someone presses the Enter key while focus is in said textbox, it will pass the Model containing any and all values to the GotZip controller action我想在 Zip 文本框上有一个事件,当有人在焦点位于所述文本框中时按下 Enter 键时,它将通过包含任何和所有值的 Model 到 GotZip Z594C103F2C6E04C0CAZAB059F031E 操作
Your action GotZip
returns a ViewResult, if you'd like to render a view to browser client while user press the Enter key in textbox, you can try to use a hidden submit button and manually trigger it to submit data to action you expect, like below.您的操作GotZip
返回一个 ViewResult,如果您想在用户按下文本框中的 Enter 键时向浏览器客户端呈现视图,您可以尝试使用隐藏的提交按钮并手动触发它以将数据提交到您期望的操作,例如以下。
<tr>
<th>
@Html.DisplayNameFor(model => model.Zip)
@Html.TextBoxFor(model => model.Zip, new { @onkeydown = "return myfunc(event);" })
</th>
</tr>
</table>
<input name="Submit" type="submit" asp-action="SubmitButton" value="Submit" />
<input id="SubmitToGotZip" type="submit" asp-action="GotZip" value="Submit" style="display:none;" />
JS function JS function
function myfunc(e) {
if (e.which == 13) {
$("#SubmitToGotZip").click();
return false;
}
return true;
}
I have looked at ajax and js, but no example showed me how to pass in a model.我查看了 ajax 和 js,但没有示例向我展示如何传入 model。
If you'd like to post form data through ajax, you can try:如果您想通过 ajax 发布表单数据,您可以尝试:
function myfunc(e) {
if (e.which == 13) {
var form = new FormData($('form')[0]);
$.ajax({
type: 'POST',
url: '/home/GotZip',
data: form,
processData: false,
contentType: false,
success: function (result) {
//code logic here
},
error: function () {
console.log('error');
}
});
return false;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.