我目前正在尝试编写一个忘记密码的页面,用户在其中输入他们的用户名、他们的电子邮件地址和一条将发送给站点管理员的消息。我想要的是以下内容:用户单击按钮后,应检查用户名和电子邮件地址是否关联。(这些值保存在我的数据库中)我设法对所有内容进行了编程,但我遇到了一个我无法解决的问题。每次 Razor 引擎呈现页面时,我都会收到 NullReferenceException 未被用户代码处理。我知道为什么会这样,但正如我所说,我无法解决这个问题。
这是代码:
@model MvcApplication1.ViewModels.ContactAdminViewModel
@using MvcApplication1.Queries
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" />
<title>SendMail</title>
</head>
<body>
@using (Html.BeginForm("SendMail", "ContactAdmin", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div>
<p>
@Html.LabelFor(m => m.username, "username")
@Html.EditorFor(m => m.username)
<p>
@Html.ValidationMessageFor(m => m.username)
</p>
</p>
<p>
@Html.LabelFor(m => m.email, "email")
@Html.EditorFor(m => m.email)
<p>
@Html.ValidationMessageFor(m => m.email)
</p>
</p>
<p>
@Html.LabelFor(m => m.message, "Your message")
<p>
@Html.TextAreaFor(m => m.message, new { cols = "35", rows = "10", @style = "resize:none" })
<p>
@Html.ValidationMessageFor(m => m.message)
</p>
</p>
</p>
<p>
<input id="send-mail" type="submit" class="button" value="Send" />
</p>
</div>
<script type="text/javascript">
$(document).ready(function () {
jQuery('#send-mail').click(function () {
@if (@DQL.CheckUsernameAndEmail(Model.username, Model.email))
{
<text>
alert("Your Message will be sent");
</text>
}
else
{
<text>
alert("Your username is not associated with the email adress");
</text>
}
});
});
</script>
}
</body>
</html>
非常感谢有关如何解决该问题的任何提示:)
编辑
DQL.cs 是一个 C# 类,我在其中写下了所有查询。它实际上是空的模型。我忘了写那个:/我真的很抱歉。然而,这里是来自 DQL.cs 的代码,它检查用户名是否与电子邮件地址相关联:
public static bool CheckUsernameAndEmail(string username, string email)
{
bool validateUser = false;
var query = from u in db.User
where (u.email.Equals(email) && u.username.Equals(username))
select u;
if (query.Count() != 0)
validateUser = true;
return validateUser;
}
这是控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.ViewModels;
using MvcApplication1.Database_Queries;
namespace MvcApplikation1.Controllers
{
public class ContactAdminController : Controller
{
[HttpGet]
public ActionResult SendMail()
{
return View();
}
[HttpPost]
public ActionResult SendMail(ContactAdminViewModel contactAdmin)
{
if (ModelState.IsValid)
{
if (DQL.CheckUsernameAndEmail(contactAdmin.username, contactAdmin.email))
{
MvcApplication1.Mail.SendMail.SendForgotPassword(contactAdmin.username, contactAdmin.email, contactAdmin.message);
return RedirectToAction("LogIn", "Account");
}
}
else
{
ModelState.AddModelError("", "Your username is not associated with the email adress");
return RedirectToAction("LogIn", "Account");
}
return RedirectToAction("LogIn", "Account");
}
}
}