0
すべてのテキストボックスを入力して[送信]ボタンをクリックしていない場合、モデルの状態エラーを追加してビューページに表示する方法(または)ビューページでこのエラーを表示する方法(すべてのフィールドは必須) asp.netでの知識は、私は最近、私はプロパティやモデルにカスタム検証を行う場合me.itはあなたに モデル状態エラーを追加してビューページに表示する方法は?
@using StackApplication.Modelsl;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Register Page</title>
</head>
<body>
<div class="header">
<div id="title">
<font color="blue"> <b> <h1>STaCK</h1> </b> </font>
</div>
<div class="sub">
<a href="" style="color:white;
padding:10px;">Log in</a>
<a href="" style="color:white;">Sign up</a>
</div>
</div>
<div style="float:left;width:100%;padding:left:10px;top:5px;">
<center>
<b><i> <h2><font color="#010100">Register Form</font></h2> </i></b>
</center>
</div>
<div id="content">
<center>
@using (Html.BeginForm(FormMethod.Post))
{
<table cellpadding="5" cellspacing="10">
<tr>
<th><h3>Username</h3></th>
<td>
@Html.TextBoxFor(m=>m.UserAccount.UserName, new { @placeholder = "Username", @id = "txtUsername", @required="required" })
</td>
</tr>
<tr>
<th><h3>Email id</h3></th>
<td>
@Html.TextBoxFor(m => m.UserAccount.Email, new { @placeholder = "Email", @id = "txtEmail", @required = "required" })
</td>
</tr>
<tr>
<th><h3>Password</h3></th>
<td>
@Html.TextBoxFor(m => m.UserAccount.Password, new { @placeholder = "Password", @id = "txtPassword", @required = "required", @type="password" })
</td>
</tr>
</table>
<input type="submit" name="register" value="REGISTER" id="buttondesign" />
<br />
<br />
}
</div>
</body>
</html>
public class StackProvider
{
public object ModelState { get; private set; }
public string CreateUserAccount(UserAccount userAccount)
{
try
{
StackRepository repository = new StackRepository();
if (string.IsNullOrEmpty(userAccount.Email) || string.IsNullOrEmpty(userAccount.Password)
|| string.IsNullOrEmpty(userAccount.UserName))
{
return "All fields are mandatory.";
}
int count = repository.GetUseraccountByEmail(userAccount.Email);
if (count > 0)
{
ModelState.AddModelError("Username already exist");
return "Username already exist";
}
repository.CreateUserAccount(userAccount);
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}
[検証の追加](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation)をご覧ください。 – Shyju