c# - 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>
解决方案
我使用几乎相同的代码:
@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>
}
你能在控制器中显示调试视图吗?
推荐阅读
- c# - 在c#中返回一个字段的数组?
- typescript - 用于依赖注入的 Typescript 中的 JSDoc typedef 模块导入等效于什么?
- postgresql - 不存在行时插入postgreql
- azure-logic-apps - 如何禁用逻辑应用的 SAS 授权?
- math - 覆盖所有 (N+1) 个点的最短距离。所有 N 个点都位于 x 轴上。剩余一点位于坐标平面的任意位置
- java - Java函数算法添加分钟到时间
- r - R使用两个不同的向量沿序列生成数字(Id),lapply?
- javascript - 角度表情符号选择器在 Windows 7 上不起作用
- java - 如何从 Maven 存储库中的依赖项中排除特定的 jar
- javascript - 尝试将图像上传到 Firebase 存储