首页 > 解决方案 > asp.net中的ajax没有从文本框中获取

问题描述

我想从文本框中检索数据,当我在文本框中输入 ID 时,它会在其他文本框中检索其余数据,这是我的代码:

控制器:

[HttpPost]
public ActionResult Contact(int id)
{   
    testEntities db = new testEntities();
    List<Table_1> tb = db.Table_1.ToList();
    var c = tb.Find(m => m.id == id);
    return View(c);
}

看法

    <div class="form-horizontal">
    <h4>Table_1</h4>
    <hr />
    <form id="myForm">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.depart, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.depart, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.depart, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="button" value="Create" class="btn btn-default" id="btnSubmit">press here</button>
            </div>
        </div>
    </form>
</div>
    
    <script type="text/javascript">
        $(document).ready(function () {
    
            $("#btnSubmit").click(function () {
                debugger
                
    
                var data = $("#myForm").serialize();
    
                $.ajax({
    
                    type: "POST",
                    url: "/Home/Contact",
                    data: data,
                    success: function (response) {
                       
                    },
    
                });
            });
    });
    </script>

标签: c#asp.netasp.net-mvc

解决方案


我使用几乎相同的代码:

@model FormSubmitWebApplication.Models.Student
@{
    ViewBag.Title = "Contact";
}

<div class="form-horizontal">
    <h4>Table_1</h4>
    <hr />
    <form id="myForm">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="button" value="Create" class="btn btn-default" id="btnSubmit">press here</button>
            </div>
        </div>
    </form>
</div>

@section  scripts {
<script type="text/javascript">
    $(document).ready(function () {

        $("#btnSubmit").click(function () {
            debugger


            var data = $("#myForm").serialize();

            $.ajax({
                type: "POST",
                url: "/Home/Contact",
                data: data,
                success: function (response) {

                },
            });
        });
    });
</script>
}

id 可以传递给控制器​​。在此处输入图像描述

你能在控制器中显示调试视图吗?


推荐阅读