简体   繁体   English

ASP.NET MVC Core TextBoxFor event pass model from View to Controller

[英]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.

相关问题 将两个或多个参数从 model 视图传递到 EF Core / ASP.NET Core MVC 应用程序中的 controller - Passing two or more parameters from model view to controller in EF Core / ASP.NET Core MVC application 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC ASP.NET 核心 MVC - 将 Model 数据从视图传递回 Controller - ASP.NET Core MVC - Passing Model Data Back to Controller from View Model 列表返回为 null 从视图到 controller Z9E0DA8438E1E38A1C30F4B76ZCE3 核心 - Model List returning as null from view to controller ASP.NET Core MVC asp.net mvc如何将完整模型从视图传递到控制器 - asp.net mvc how to pass full model from view to controller 单击 ASP.NET MVC 中的按钮时,如何将 model 值从视图传递到 controller - How to pass model value from a view to a controller when clicking on the button in ASP.NET MVC 如何将自定义模型对象从剃刀视图传递到ASP.NET MVC 5中的控制器操作 - How to pass custom model object from razor view to controller action in ASP.NET MVC 5 ASP.NET MVC传递多个模型从控制器查看 - ASP.NET MVC Pass Multiple Models to View From Controller 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 在ASP.NET MVC 5中将整个对象从视图传递到控制器 - Pass entire object from view to controller in ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM