Ich bin neu bei MVC und erstelle ein Registrierungsformular für meine App, aber mein Klick auf den Button funktioniert nicht. Der aktuelle Code ist unten nicht angegeben
aussicht
<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>
@Html.Label("User Name")
</td>
<td>
@Html.TextBoxFor(account => account.Username)
</td>
</tr>
<tr>
<td>
@Html.Label("Email")
</td>
<td>
@Html.TextBoxFor(account => account.Email)
</td>
</tr>
<tr>
<td>
@Html.Label("Password")
</td>
<td>
@Html.TextBoxFor(account => account.Password)
</td>
</tr>
<tr>
<td>
@Html.Label("Confirm Password")
</td>
<td>
@Html.Password("txtPassword")
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSubmit" value="Sign Up" />
</td>
</tr>
</table>
</fieldset>
modell-
public class Account
{
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
controller (nicht vollständig abgeschlossen)
public class AccountController : Controller
{
//
// GET: /Account/
public ActionResult Index()
{
return View();
}
// GET: /Account/SignUp
public ActionResult SignUp()
{
return View();
}
[HttpPost]
public ActionResult SignUp(string userName,string email,string password)
{
Account createAccount = new Account();
createAccount.Username = userName;
createAccount.Email = email;
createAccount.Password = password;
return View("Index");
}
}
wie definiere ich das Klickereignis hier? Ich habe es mit dem http-Post versucht, aber es funktioniert nicht. Ich weiß, dass mein Code nicht korrekt ist. Bitte zeigen Sie, was hier der Fehler ist
ASP.NET MVC funktioniert nicht bei Ereignissen wie ASP classic; Es gibt kein "Klickereignis". Ihre Controller-Methoden entsprechen den Anforderungen, die an den Server gesendet werden.
Stattdessen müssen Sie das Formular wie folgt in Code einbetten:
@using (Html.BeginForm("SignUp", "Account", FormMethod.Post))
{
<!-- form goes here -->
<input type="submit" value="Sign Up" />
}
Dadurch wird ein Formular erstellt, und Ihre Eingabe wird einen POST auslösen, der Ihre SignUp()
-Methode trifft, vorausgesetzt, Ihre Routen sind ordnungsgemäß eingerichtet (die Standardeinstellungen sollten funktionieren).
wie in der Antwort von @anaximander, aber Ihre Anmeldeaktion sollte eher aussehen
[HttpPost]
public ActionResult SignUp(Account account)
{
if(ModelState.IsValid){
//do something with account
return RedirectToAction("Index");
}
return View("SignUp");
}
sie können diesen Code ausprobieren
@using (Html.BeginForm("SignUp", "Account", FormMethod.Post)){<fieldset>
<legend>Sign Up</legend>
<table>
<tr>
<td>
@Html.Label("User Name")
</td>
<td>
@Html.TextBoxFor(account => account.Username)
</td>
</tr>
<tr>
<td>
@Html.Label("Email")
</td>
<td>
@Html.TextBoxFor(account => account.Email)
</td>
</tr>
<tr>
<td>
@Html.Label("Password")
</td>
<td>
@Html.TextBoxFor(account => account.Password)
</td>
</tr>
<tr>
<td>
@Html.Label("Confirm Password")
</td>
<td>
@Html.Password("txtPassword")
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSubmit" value="Sign Up" />
</td>
</tr>
</table>
</fieldset>}
MVC macht keine Events. Fügen Sie einfach ein Formular und eine Schaltfläche zum Senden auf der Seite ein, und die mit dem Attribut HttpPost versehene Methode verarbeitet diese Anforderung.
Vielleicht möchten Sie ein oder zwei Lernprogramme zum Erstellen von Ansichten, Formularen und Controllern lesen.