asp.net - 从视图到控制器的 POST 命令不起作用
问题描述
有时我根本不明白。我只想创建一个表单,用户可以在其中输入一个字段,必须选中一个复选框,并且只有在选中该复选框时,提交按钮才有效。所以我的模型看起来像这样:
[Table("ResetType")]
public class ResetTypeModel
{
public bool IsTrue => true;
[Display(Name = "Hostname")]
[Required]
[StringLength(11, MinimumLength=11)]
[RegularExpression("^[a-zA-Z]{2}[a-zA-Z]{1}[a-zA-Z0-9]{2}[0-9]{3}[a-zA-Z]{3}$")]
public string hostname {get; set;}
[Required]
[RegularExpression("(True|true)")]
[Display(Name = "I agree to the terms and conditions")]
[Compare(nameof(IsTrue), ErrorMessage = "Please agree to Terms and Conditions")]
public bool confirmed {get; set;}
}
}
我的控制器调用视图:
public IActionResult Reset()
{
return View();
}
最后是我目前的观点:
@using (Html.BeginForm("ResetDevice", "Test", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="media-body">
<label for="resettype_name">Reset Type</label>
<select class="form-control" data-val="true" id="ddl_resettype" ><option value="">Please select a Reset Type</option>
<option value="Delete">Delete</option>
<option value="Reset">Reset</option>
</select>
</div>
<div class="media-body" >
<label for="label_hostname">Hostname</label>
<input class="form-control" name="hostname" id="hostname" />
@Html.LabelFor(model=> model.hostname)
@Html.TextBoxFor(m => m.hostname, new { @class = "form-control" })
@* <input asp-for="model.hostname" class="form-control" name="hostname" id="hostname" /> *@
@* <input type="checkbox" id="Authorized" name="Authorized" /> *@
@Html.CheckBoxFor(x => x.confirmed)
@Html.LabelFor(m => m.confirmed)
@Html.EditorFor(model => model.confirmed)
@Html.ValidationMessageFor(m => m.confirmed)
</div>
<input type="submit" value="Reset" class="btn btn-primary" />
}
@section Scripts
{
<script src="~/js/jquery-3.5.1.min.js" defer></script>
<script src="~/js/jquery.validate.js" defer></script>
当我单击提交按钮时,没有任何内容得到验证,但我想我在过去的几天里用另一个视图有过一次,其次,更重要的是,当我对控制器进行 POST 时,我得到一个 404
[HttpPost]
//[Route("ResetDevice")]
[ValidateAntiForgeryToken]
public IActionResult ResetDevice([FromBody] ResetTypeModel rtm){
return RedirectToAction("Reset", "Test");
}
就像没有调用该方法一样。
我错过了什么?
更新:
当我将其称为 ajax 时,它正在工作,那么我错过了什么?
$.ajax({
type: "POST",
url: 'ResetDevice',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
headers:
{
"RequestVerificationToken": $('input:hidden[name="__RequestVerificationToken"]').val()
},
traditional: true,
success: function () {
alert("Successfully Imported");
},
error: function (jqXHR, textStatus, errorThrown) {
alert("jqXHR:" + jqXHR.status + " errorThrown: " + errorThrown);
}
更新:
好的,我想我发现了错误。在控制器中,我使用 FromBody 而不是 FromForm
解决方案
我在控制器中使用了 FromBody 而不是 FromForm。所以这取决于你是使用 javascript ajax 还是 html.beginform
推荐阅读
- api - Wiki.js GraphQL:创建页面示例
- vue.js - 使用 v-show 时组件会重新渲染?VueJS
- python - 为什么 Django 没有创建 DB 模型的 dateTime 字段?
- haskell - 从 GHCi 隐藏随机版本
- javascript - Mongoose find() 检查是否有任何值匹配
- c++ - 基于非类型参数的类模板重载?
- c++ - 如何使用迭代器或c ++中的任何其他方式遍历元组向量
- java - Spring Boot - 如何通过实现 BeforeAllCallback 的自定义扩展类设置或覆盖 application.yml 中定义的属性?
- python - 根据另一个数据框中的列填充一个数据框中的空值?
- python - 条形图(主要)然后折线图(次要)工作正常,但如果我改变代码中的序列则不起作用