【发布时间】:2021-07-22 15:11:40
【问题描述】:
我在 asp.net 核心中有一个代码,它从数据库的“颜色”表中读取所有行(在 SQL 服务器中)并在数据表中显示这些数据。在该表上方,点击后有一个按钮,会打开一个手风琴并显示一个可以在下表中插入新行的表单。
我有一个名为“ColorController”的控制器,它有 3 个动作:ShowColors、Insert(get mode)、Insert(post mode)。
ColorController.cs:
private IColor _icolor;
public ColorController(IColor icolor)
{
_icolor = icolor;
}
public IActionResult ShowColors()
{
return View(_icolor.ShowColors());
}
public IActionResult Insert()
{
return View();
}
[HttpPost]
public IActionResult Insert(ColorViewModel colorViewModel)
{
if (ModelState.IsValid)
{
if (_icolor.isColorExist(colorViewModel.ColorCode))
{
ViewBag.Validation = 1;
ModelState.AddModelError("ColorCode", "Exist!");
return View(colorViewModel);
}
else
{
ViewBag.Validation = 0;
// ... Insert codes
return RedirectToAction("ShowColors","Color");
}
}
else
{
ViewBag.Validation = 1;
return View(colorViewModel);
}
}
我为颜色控制器的操作编写了 2 个视图。这些观点:
ShowColors.cshtml:
@model IEnumerable<DataAccessLayer.Entities.Color>
<div class="card">
<div class="card-body">
<div class="accordion">
<div class="accordion-row">
<a class="accordion-header" href="#">
<button type="button" onclick='Insert() >
InsertColor
</button>
</a>
<div class="accordion-body" id="InsertForm">
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-body">
<table id="example" class="table table-hover">
@*...Show datatable...*@
</table>
</div>
</div>
</div>
<script>
function Insert() {
var _url = '@Url.Action("Insert", "Color")';
$.ajax({
url: _url,
type: "Get",
data: {}
}).done(function (result) {
$('#InsertForm').html(result);
});
}
</script>
Insert.cshtml(我使用“IsValid”输入进行检查验证。)
<div class="card">
<div class="card-body">
<form>
<input name="IsValid" type="hidden" value="@ViewBag.Validation" id="validation" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-row">
<div class="col-md-6 mb-3">
<div class="form-group">
<label asp-for="ColorCode" class="control-label"></label>
<input asp-for="ColorCode" class="form-control" id="txtColorCode" />
<span asp-validation-for="ColorCode" class="text-danger"></span>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label asp-for="ColorName" class="control-label"></label>
<input asp-for="ColorName" class="form-control" id="txtColorName" />
<span asp-validation-for="ColorName" class="text-danger"></span>
</div>
</div>
</div>
<div class="form-group">
<div class="ml-3 mt-3">
<button type="submit" onclick='InsertColor()' >Add Color</button>
</div>
</div>
</form>
</div>
</div>
<script>
function InsertColor() {
var _url = '@Url.Action("Insert", "Color")';
$.ajax({
url: _url,
type: "POST",
data: {
ColorCode: $("#txtColorCode").val(),
ColorName: $("#txtColorName").val(),
},
success: function (response) {
$('#InsertForm').html(response);
var isValid = $('body').find('[name="IsValid"]').val();
if (isValid == 1) {
stop();
}
},
complete: function (xhr, status) {
var isValid = $('body').find('[name="IsValid"]').val();
console.log("Is valid =" + isValid);
if (isValid != 1)
{
if (status == 'success')
window.location.href = '@Url.Action("ShowColors", "Color")';
}
},
}).done(function (result) {
$('#InsertForm').html(result);
var isValid = $('body').find('[name="IsValid"]').val();
if (isValid != 1) {
window.location.href = '@Url.Action("ShowColors", "Color")';
}
});
}
</script>
当我运行代码时,一切正常,直到我想插入新颜色。 GET 模式(插入操作)工作正常,但 POST 模式有问题。在我第一次添加新颜色和模型状态无效时,代码不起作用。下次,如果模型状态有效,则在数据库中插入新行,但在“ShowColors”视图的表中看不到。我在代码中重新加载 showColors 动作,但在地址栏中,地址有参数,如下所示:
localhost:.../Color/ShowColors?IsValid=&ColorCode=pur&ColorName=purple
虽然我想展示这个:
localhost:.../Color/ShowColors
我猜“插入”视图中的 ajax 函数是错误的,但我找不到错误。谁能帮帮我,问题出在哪里?
【问题讨论】:
标签: javascript c# jquery ajax asp.net-core