当我尝试访问signUp时(如我在调试中看到的那样),当代码到达视图时出现问题。 我正在获取运行时异常“ System.Web.Mvc.WebViewPage.Model.get返回null。” 请帮助
这是完整的失败:
System.NullReferenceException:'对象引用未设置为对象的实例。'
System.Web.Mvc.WebViewPage.Model.get返回null。
在这里发生
@{
ViewBag.Title = "SignUp";
}
SignUp.cshtml
<body>
<div class="signup-form">
<form class="signup-form" action="SignupSubmit">
<div class="col-md-8 order-md-1">
<h2>SignUp</h2>
<h3>Create an account</h3>
<h4 class="mb-3">Please complete your details : </h4>
<div class="row">
<div class="col-md-4 mb-3">
<label for="firstName">Id number</label>
<input type="text" class="form-control" name="userid" id="idnum" value="@Model.userid"> @Html.ValidationMessageFor(u => u.userid)
<div class="invalid-feedback">
Valid id number is required.
</div>
</div>
<div class="col-md-4 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" name="firstName" id="firstName" value="@Model.firstName"> @Html.ValidationMessageFor(u => u.firstName)
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-md-4 mb-3">
<label for="lastName">Last name</label>
<input type="text" class="form-control" name="lastName" id="lastName" value="@Model.lastName"> @Html.ValidationMessageFor(u => u.lastName)
<div class="invalid-feedback">
Valid last name is required.
</div>
</div>
</div>
<div class="mb-3">
<label for="email" class="col-sm-2 col-form-label">Email </label>
<input type="email" class="form-control" name="email" id="inputEmail" placeholder="you@example.com" value="@Model.email"> @Html.ValidationMessageFor(u => u.email)
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="mb-3">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<input type="password" class="form-control" name="password" id="inputPassword" placeholder="Password" value="@Model.password"> @Html.ValidationMessageFor(u => u.password)
<div class="invalid-feedback">
Please enter a valid password with numbers and letters.
</div>
</div>
<label class="checkbox-inline"><input type="checkbox" required="required"> I accept the <a href="#">Terms of Use</a> & <a href="#">Privacy Policy</a></label>
<hr class="mb-4">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
@Html.ActionLink("Already have an account? Login here. ", "SignIn", "User", new { area = "" }, new { @class = "navbar-brand" })
</div>
</form>
</div>
UserController.cs
public ActionResult SignUp()
{
return View(new User());
}
public ActionResult SignupSubmit(User user)
{
if (ModelState.IsValid)
{
AddUserToDB(user);
}
else
{
return View("SignUp");
}
TempData["Falied"] = "You can now connect with your details";
return RedirectToAction("SignIn", "User");
}
[HttpPost]
public bool AddUserToDB(User u)
{
try
{
UserDal dal = new UserDal();
dal.users.Add(u);
dal.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var errors in ex.EntityValidationErrors)
{
foreach (var validationError in errors.ValidationErrors)
{
// get an error message
string errorMessage = validationError.ErrorMessage;
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
return false;
}
return true;
}
User.cs
public class User
{
[Key]
[Required]
[RegularExpression("^[0-9]{9}$", ErrorMessage = "ID must be with 9 numbers")]
public string userid { get; set; }
[Required]
[RegularExpression("^[a-z]+$", ErrorMessage = "First Name must be only Characters")]
public string firstName { get; set; }
[Required]
[RegularExpression("^[a-z]+$", ErrorMessage = "Last Name must be only Characters")]
public string lastName { get; set; }
[Required]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Email must conatain characters seperate with @")]
[StringLength(100, MinimumLength = 4, ErrorMessage = "Email must be with at least 4 characters or Maximum 50 caracters")]
public string email { get; set; }
[Required]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$", ErrorMessage = "Password must conatain eight characters, at least one letter and one number")]
[StringLength(8, MinimumLength = 8, ErrorMessage = "Password must be with 8 Characters")]
public string password { get; set; }
public string permission { get; set; }
public float totalHoures { get; set; }
}
答案 0 :(得分:0)
问题在于用户未在此处发送...
else {
return View("SignUp");
}
所以我将代码更改为
return View("SignUp",user);
,现在它按预期运行。 我的视图是强类型视图,我只想在未成功通过验证的文本框旁边查看错误消息。