I am currently trying to program a forgot password page where the user enter their username, their email address and a message which will be sent to the administrator of the site. Want I want is the following: After the user clicks on the button the username and the email address should be checked if they are associated. (Those values are saved in my database) I managed to programm everything but I have a problem which I cannot solve. Everytime the Razor engine renders the page I'll get a NullReferenceException was unhandled by user code. I know why this is happening but as I said I cannot fix this.
Here is the code:
@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>
Any tips on how to solve that problem are highly appreciated :)
EDIT
The DQL.cs is a C# Class where I wrote down all my queries. It's actually the model that is null. I forgot to write that :/ I'm really sorry. Nevertheless here is the code from the DQL.cs which checks if the username is associated with the email address:
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;
}
This the Controller code:
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");
}
}
}
NullReferenceExceptiongetting thrown on?