0
您好我想打一個登錄頁面爲會員和管理員還.. 在這裏,我也做了下面的代碼爲:獲取對象空refrence錯誤在登錄在asp.net MVC4
在型號:
public class Login
{
public int ID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
控制器
[HttpPost]
public ActionResult Login(Login login, MemberDetailsWrapper memberlogin)
{
WrapperDB db = new WrapperDB();
if (login.UserName.Equals("admin") &&login.Password.Equals("admin")) //This will check for admin
{
returnRedirectToAction("Index");
}
else if
(
(login.UserName.Equals(memberlogin.objMemberBasicInformation.MemberFirstName.Equals(db.MemberBasicInformationDBS.Where(a =>a.MemberFirstName.Equals(a.MemberFirstName))))
&& (login.Password.Equals(memberlogin.objMemberBasicInformation.Qualification.Equals(db.MemberBasicInformationDBS.Where(a=>a.Qualification.Equals(a.Qualification))))))
)
//This is For Member Login When Member Enter His FistName as a userName and Qualification as Password then Both credential match with the data present in db and return the respected person information
{
return RedirectToAction("Index","MemberDetails");
}
ModelState.AddModelError("", "UserName And Password is Incorrect!!");
return View();
}
鑑於:
<%@PageTitle=""Language="C#"Inherits="System.Web.Mvc.ViewPage<SHGManagementProject.Models.Login>"%>
<h2>Login</h2>
<%using (Html.BeginForm("Login", "Home", FormMethod.Post))
{ %>
<%:Html.ValidationSummary(true) %>
<fieldset>
<legend>Log in Form</legend>
<div>
UserName
<%:Html.TextBoxFor(m=>m.UserName) %>
</div>
<br/>
<div>
Password
<%:Html.PasswordFor(m=>m.Password) %>
</div>
<br/>
<div>
<inputid="SubmitLogin"type="submit"value="Login"/>
<%--<%:Html.ActionLink("Are you Member? click Here!!","Login","Account") %>--%>
</div>
</fieldset>
<%} %>
在執行時當我輸入用戶名,並通過爲管理員,然後它會成功執行..但是當我進入另一個用戶名和密碼,然後它拋出錯誤的NullReferenceException是未處理的用戶代碼
請告訴我的解決方案,並幫助我解決這個
你是否已經通過調試器中的代碼來查看你在哪裏得到錯誤? – christiandev
請參見[什麼是NullReferenceException,以及如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-it)。 'memberlogin.objMemberBasicInformation.MemberFirstName'最有可能是'null'。 – CodeCaster
指出導致錯誤的行 –