Wenn Benutzer nicht alle Textfeld ausfüllen und klicken Sie auf Absenden, dann, wie Sie Modellstatusfehler hinzufügen und in Ansicht anzeigen (oder), wie dieser Fehler angezeigt wird (alle Felder sind obligatorisch) in Ansicht page.i nicht gut Kenntnisse in asp.net, ich das vor kurzem erfahren, so wenden Sie sich bitte helfen me.it für me..thanking Sie sehr hart Wie man Modellstatusfehler hinzufügt und in Ansichtsseite zeigt?
@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;
}
}
Werfen Sie einen Blick auf [Hinzufügen Validierung] (https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/validation) – Shyju