Ich habe einige Tutorials gefolgt und ich habe auch StackOverflow durchforstet, aber ich komme immer noch in einer Sackgasse.ViewModel Werte sind immer Null
Im Grunde versuche ich ein Formular in MVC 5 (neu in MVC, WebForms schwer verwendet, obwohl). So, hier sind meine Komponenten -
Controller (./Controllers/AccountController.cs)
using BitHost.Models;
using System.Web.Mvc;
namespace BitHost.Controllers
{
public class AccountController : Controller
{
// GET: Account
public ActionResult Index()
{
return View();
}
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(UserAccount model)
{
string sEmailAddress = model.EmailAddress;
string sPassword = model.Password;
bool bAgreedToTerms = model.AgreedToTerms;
return Redirect("/");
}
}
}
Viewmodel (./Models/AccountModel.cs)
namespace BitHost.Models
{
public class UserAccount
{
public string EmailAddress;
public string Password;
public bool AgreedToTerms;
}
}
anzeigen (./Views/Account/Register.cshtml)
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Register";
}
@model BitHost.Models.UserAccount
<div class="row">
<h2>Create a new account</h2>
@using (Html.BeginForm())
{
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></span>
@Html.TextBoxFor(model => model.EmailAddress, new { Class = "form-control", Placeholder = "Email Address" })
</div><br />
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></span>
@Html.TextBoxFor(model => model.Password, new { Class = "form-control", Type = "password", Placeholder = "Password" })
</div><br />
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></span>
<input type="password" class="form-control" placeholder="Verify Password" />
</div><br />
<div class="input-group">
@Html.CheckBoxFor(model => model.AgreedToTerms, new { Class = "form-control" })
</div><br />
<input type="submit" name="Submit" class="btn btn-default" />
}
</div>
auf „Senden“ auf dem Anmeldeformular Nach einem Klick, ich auf die Aktion [Httppost] getroffen bekommen, das Konto zu erstellen, aber die Variablen aus dem Modell alle Nullwerte enthalten gesammelt.
Jede Hilfe wäre dankbar, danke.
Ich bin nicht sicher, warum es Nullwerte veröffentlichen, aber Sie werden wollen Ihre Htmlattributes Parameter ändern in Kleinbuchstaben, z.B. 'new {@class =" form-control ", type =" password ", placeholder =" Password "}' – jrummell
mögliches Posten von Nullwerten, weil Sie in Ihrem Controller nichts speichern? Sie initialisieren und sagen sofort 'return Redirect'. –
@AgustinMeriles seine Aktionsmethoden können gleich benannt werden, da sein erstes 'Register ActionResult'' HttpGet' ist und das andere 'Register ActionResult'' HttpPost' ist. –