C# 向te视图显示ModelState错误
我试图向用户显示一些错误 为此,我正在使用C# 向te视图显示ModelState错误,c#,html,asp.net,modelstate,C#,Html,Asp.net,Modelstate,我试图向用户显示一些错误 为此,我正在使用ModelState.isValid和@Html.ValidationSummary来显示它 但不知何故,它目前不起作用 型号: [Required(ErrorMessage = "Account name is required")] [EmailAddress] public string CTC_accountName { get; set; } [Required(ErrorMessage = "Password is required")]
ModelState.isValid
和@Html.ValidationSummary
来显示它
但不知何故,它目前不起作用
型号:
[Required(ErrorMessage = "Account name is required")]
[EmailAddress]
public string CTC_accountName { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string CTC_accountPassword { get; set; }
[HttpPost]
public async Task<IActionResult> Submit(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
}
}
@using (Html.BeginForm("Submit", "Register", FormMethod.Post, new { id = "formAll", style = "margin-top:3%", enctype = "multipart/form-data" }))
{
<div class="shadow-sm p-2 mb-3 rounded">
<div class="col-sm-9">
<h5>Personal Details</h5>
</div>
</div>
<div id="formMember">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountName, "Account/Email*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @id = "CTC_accountName", @autocomplete = "username" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountPassword, "Password*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Password", @id = "CTC_accountPassword", @type = "password", @autocomplete = "current-password" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
</div>
}
控制器:
[Required(ErrorMessage = "Account name is required")]
[EmailAddress]
public string CTC_accountName { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string CTC_accountPassword { get; set; }
[HttpPost]
public async Task<IActionResult> Submit(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
}
}
@using (Html.BeginForm("Submit", "Register", FormMethod.Post, new { id = "formAll", style = "margin-top:3%", enctype = "multipart/form-data" }))
{
<div class="shadow-sm p-2 mb-3 rounded">
<div class="col-sm-9">
<h5>Personal Details</h5>
</div>
</div>
<div id="formMember">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountName, "Account/Email*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @id = "CTC_accountName", @autocomplete = "username" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountPassword, "Password*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Password", @id = "CTC_accountPassword", @type = "password", @autocomplete = "current-password" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
</div>
}
[HttpPost]
公共异步任务提交(RegisterViewModel模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
}
}
查看:
[Required(ErrorMessage = "Account name is required")]
[EmailAddress]
public string CTC_accountName { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
public string CTC_accountPassword { get; set; }
[HttpPost]
public async Task<IActionResult> Submit(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
}
}
@using (Html.BeginForm("Submit", "Register", FormMethod.Post, new { id = "formAll", style = "margin-top:3%", enctype = "multipart/form-data" }))
{
<div class="shadow-sm p-2 mb-3 rounded">
<div class="col-sm-9">
<h5>Personal Details</h5>
</div>
</div>
<div id="formMember">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountName, "Account/Email*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email", @id = "CTC_accountName", @autocomplete = "username" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">
@Html.LabelFor(model => model.CTC_accountPassword, "Password*")
</div>
<div class="col-sm-10">
@Html.EditorFor(model => model.CTC_accountPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Password", @id = "CTC_accountPassword", @type = "password", @autocomplete = "current-password" } })
</div>
<div class="col-sm-10">
@Html.ValidationMessageFor(model => model.CTC_accountName)
</div>
</div>
</div>
}
@使用(Html.BeginForm(“Submit”、“Register”、FormMethod.Post、new{id=“formAll”、style=“margin top:3%”、enctype=“multipart/form data”}))
{
个人资料
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.LabelFor(model=>model.CTC_accountName,“Account/Email*”)
@EditorFor(model=>model.CTC_accountName,new{htmlAttributes=new{@class=“form control”,@placeholder=“Email”,@id=“CTC_accountName”,@autocomplete=“username”})
@Html.ValidationMessageFor(model=>model.CTC\u accountName)
@Html.LabelFor(model=>model.CTC\u accountPassword,“Password*”)
@EditorFor(model=>model.CTC_accountPassword,new{htmlAttributes=new{@class=“form control”、@placeholder=“Password”、@id=“CTC_accountPassword”、@type=“Password”、@autocomplete=“current Password”})
@Html.ValidationMessageFor(model=>model.CTC\u accountName)
}
我很感激你的帮助我发现了问题所在
我不得不删除提交函数中的POST-call-ajax,以查看@Html.ValidationSummary 您的视图名为“Submit.cshtml”吗?@howcheng不,我的视图名为Register.cshtmlh这可能是您的问题。如果返回
视图(模型)
,则ASP.NET将查找具有相同操作名称的视图。您需要返回视图(“Register”,model)
而不是.Thx。遗憾的是,它仍然不起作用。