gpt4 book ai didi

asp.net-mvc - MVC模型验证

转载 作者:行者123 更新时间:2023-12-03 08:26:21 24 4
gpt4 key购买 nike

因此,我目前正在构建需要验证用户模型的应用程序,如果用户填写了不正确的属性,它将告诉他们。
我已经设置了数据注释,但是我不确定如何将错误消息转发回用户?
到目前为止,我已经在模型和 View 上进行了设置。

模型

public class DatabaseModel
{
[Required(ErrorMessage = ("A first name is required"))]
public string FirstName { get; set; }
[Required(ErrorMessage = ("A last name is required"))]
public string LastName { get; set; }
[Required(ErrorMessage = ("A valid role is required"))]
public string Role { get; set; }
// TODO - Validate rank to only b 1 - 10
//
[Range(1,10, ErrorMessage = ("A rank between 1 and 10 is required"))]
public int Rank { get; set; }

}

和 View
@model RoleCreatorAndEditor.Models.DatabaseModel
@{
ViewData["Title"] = "Index";
}

<h2>User Information</h2>

<p>This is your user information!</p>

@using (Html.BeginForm("Index", "Home", FormMethod.Post)) {
@Html.Label("First Name")
<br>
@Html.TextBoxFor(m => m.FirstName)
<br>
@Html.Label("Last Name")
<br>
@Html.TextBoxFor(m=>m.LastName)
<br>
@Html.Label("Role")
<br>
@Html.TextBoxFor(m => m.Role)
<br>
@Html.Label("Rank")
<br>
@Html.TextBoxFor(m => m.Rank)
<br><br>
<input type="submit" value="Save">
}

我的 Controller
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
DatabaseModel model = new DatabaseModel();
return View(model);
}
[HttpPost]
public ActionResult Index(DatabaseModel model)
{
if (ModelState.IsValid)
{
ListToDatatable convert = new ListToDatatable();
DataTable user = convert.Convert(model);
DatabaseRepository dbRepo = new DatabaseRepository();
dbRepo.Upload(user);
}
return View();
}
}

我认为需要将模型传递回 View 以显示错误消息,尽管我已阅读过asp.net上的文档,但我无法理解他们是如何添加错误消息的,并且表单知道如何显示错误消息。给用户的错误。

我非常困惑。

最佳答案

您需要在Controller中使用ModelState.IsValid,并在 View 中使用@Html.ValidationMessageFor(model => model.FirstName):

public ActionResult Index(ViewModel _Model) 
{
// Checking whether the Form posted is valid one.
if(ModelState.IsValid)
{
// your model is valid here.
// perform any actions you need to, like database actions,
// and/or redirecting to other controllers and actions.
}
else
{
// redirect to same action
return View(_Model);
}
}

例如:
@model RoleCreatorAndEditor.Models.DatabaseModel
@{
ViewData["Title"] = "Index";
}

<h2>User Information</h2>

<p>This is your user information!</p>
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) {
@Html.LabelFor(m=>m.FirstName)
<br>
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })

<br>
@Html.LabelFor(m=>m.LastName)
<br>
@Html.TextBoxFor(m=>m.LastName)
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })

. . .

<input type="submit" value="Save">
}

Controller :
[HttpPost]
public ActionResult Index(DatabaseModel model)
{
if (ModelState.IsValid)
{
ListToDatatable convert = new ListToDatatable();
DataTable user = convert.Convert(model);
DatabaseRepository dbRepo = new DatabaseRepository();
dbRepo.Upload(user);
}
return View(model);
}

关于asp.net-mvc - MVC模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40280031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com