ASP.NET MVC Web 应用程序 - 面临空指针异常
ASP.NET MVC Web Application - Facing Null Pointer Exception
每当我尝试在 Employee
数据图像上调用此重定向 link 时,我都会收到此异常。非常感谢任何可以解决此问题的帮助。
我的作品:
Employee
查看 empRecords
:
@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
@{
HttpCookie cookie = Request.Cookies["Detalis"];
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EmpRecords</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div style="float:left">
@Html.ActionLink("Create New", "Create")
</div>
<div style="float:right">
@{
String img64 = (string)Session["userImage"];
String img64Url = string.Format("data:image/" +
(string)Session["userImageType"] + ";base64,{0}", img64);
//imagetype can be e.g. gif, jpeg, png etc.
}
<img alt="" src="@img64Url" width="45" height="80"
class="rounded-circle" />
<br />
@Html.ActionLink("Click to Logout", "Logout", "User")
</div>
<br />
<p>
@Html.Partial("_EmployeeInfo", Model)
</p>
<br />
<div style="float:left">
<dl>
<dt>
@Html.ActionLink("User Last Login:", cookie["Lastlogin"])
</dt>
<dd>
@if (cookie != null)
{
@cookie["Lastlogin"];
}
</dd>
</dl>
</div>
<br />
</body>
</html>
局部布局视图_EmployeeInfo
:
@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.First().FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ImageURL)
</th>
<th>
Image
</th>
</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="6">
No records match search criteria
</td>
</tr>
}
else
{
foreach (var item in Model)
{
using (Html.BeginForm("Delete", "Employee", new
{
id =
item.EmployeeID
}))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImageURL)
</td>
<td>
@if (!string.IsNullOrEmpty(item.ImageURL))
{
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
}
else
{
<span>No Image Found!</span>
}
</td>
</tr>
}
}
}
</table>
@Html.PagedListPager(Model, page => Url.Action("empRecords", new { page,
SearchBy = Request.QueryString["SearchBy"], SearchTerm =
Request.QueryString["SearchTerm"], sortBy =
Request.QueryString["sortBy"] }), new PagedListRenderOptions() {
Display = PagedListDisplayMode.IfNeeded,
DisplayPageCountAndCurrentLocation = true })
所以,主要问题是每当我尝试调用此重定向时 link 我都会遇到此错误:
代码:
@if (!string.IsNullOrEmpty(item.ImageURL))
{
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
}
错误信息:
Employee
用于以下任务的控制器函数:
public ActionResult empRecords(string SearchBy, string SearchTerm, int? page, string sortBy)
{
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
List<Employee> list = entity.GetList(SearchBy,SearchTerm, sortBy);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(list.ToPagedList(page ?? 1, 5));
}
public ActionResult Details(long id)
{
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
Employee employee = entity.GetSingleEmployee(id);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(employee);
}
Employee
查看 Details
:
@model My_Work.Models.Employee
@{
Layout = null;
HttpCookie cookie = Request.Cookies["Detalis"];
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Employee Detail</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div>
<h4>Employee Detail</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.EmployeeID)
</dt>
<dd>
@Html.DisplayFor(model => model.EmployeeID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dd>
@{
String img64 = (string)Session["userImage"];
String img64Url = string.Format("data:image/" +
(string)Session["userImageType"] + ";base64,{0}", img64);
//imagetype can be e.g. gif, jpeg, png etc.
}
<img alt="" src="@img64Url" width="45" height="80"
class="rounded-circle" />
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeID
}) |
@Html.ActionLink("Back to List", "empRecords")
</p>
</body>
</html>
问题
您会收到上述错误,因为 id
是 Employee
控制器中 Details
函数(操作)中的必需参数。
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
解决方案
您必须确保 id
必须包含在 URL 中才能重定向到 Employee/Details
。
URL应该如下所示:
/Employee/Details/{id}
解决方案 1:
<a href="/Employee/Details/@item.EmployeeID">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
解决方案 2:
<a href="@Url.Action("Details", "Employee", new { id = item.EmployeeID})">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
补充说明,你也可以让id参数不是必须的,这样就不会给你空指针异常了。在您的代码中,如果您希望 id 参数不能为 null,则需要添加一些验证代码。
public ActionResult Details(long? id = null)
{
// validation code example
if (!id.HasValue)
{
// Redirect or return the appropriate HTTP code here
}
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
Employee employee = entity.GetSingleEmployee(id);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(employee);
}
每当我尝试在 Employee
数据图像上调用此重定向 link 时,我都会收到此异常。非常感谢任何可以解决此问题的帮助。
我的作品:
Employee
查看 empRecords
:
@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
@{
HttpCookie cookie = Request.Cookies["Detalis"];
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>EmpRecords</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div style="float:left">
@Html.ActionLink("Create New", "Create")
</div>
<div style="float:right">
@{
String img64 = (string)Session["userImage"];
String img64Url = string.Format("data:image/" +
(string)Session["userImageType"] + ";base64,{0}", img64);
//imagetype can be e.g. gif, jpeg, png etc.
}
<img alt="" src="@img64Url" width="45" height="80"
class="rounded-circle" />
<br />
@Html.ActionLink("Click to Logout", "Logout", "User")
</div>
<br />
<p>
@Html.Partial("_EmployeeInfo", Model)
</p>
<br />
<div style="float:left">
<dl>
<dt>
@Html.ActionLink("User Last Login:", cookie["Lastlogin"])
</dt>
<dd>
@if (cookie != null)
{
@cookie["Lastlogin"];
}
</dd>
</dl>
</div>
<br />
</body>
</html>
局部布局视图_EmployeeInfo
:
@using PagedList.Mvc;
@using PagedList;
@model IPagedList<My_Work.Models.Employee>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.First().FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ImageURL)
</th>
<th>
Image
</th>
</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="6">
No records match search criteria
</td>
</tr>
}
else
{
foreach (var item in Model)
{
using (Html.BeginForm("Delete", "Employee", new
{
id =
item.EmployeeID
}))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImageURL)
</td>
<td>
@if (!string.IsNullOrEmpty(item.ImageURL))
{
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
}
else
{
<span>No Image Found!</span>
}
</td>
</tr>
}
}
}
</table>
@Html.PagedListPager(Model, page => Url.Action("empRecords", new { page,
SearchBy = Request.QueryString["SearchBy"], SearchTerm =
Request.QueryString["SearchTerm"], sortBy =
Request.QueryString["sortBy"] }), new PagedListRenderOptions() {
Display = PagedListDisplayMode.IfNeeded,
DisplayPageCountAndCurrentLocation = true })
所以,主要问题是每当我尝试调用此重定向时 link 我都会遇到此错误:
代码:
@if (!string.IsNullOrEmpty(item.ImageURL))
{
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
}
错误信息:
Employee
用于以下任务的控制器函数:
public ActionResult empRecords(string SearchBy, string SearchTerm, int? page, string sortBy)
{
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
List<Employee> list = entity.GetList(SearchBy,SearchTerm, sortBy);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(list.ToPagedList(page ?? 1, 5));
}
public ActionResult Details(long id)
{
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
Employee employee = entity.GetSingleEmployee(id);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(employee);
}
Employee
查看 Details
:
@model My_Work.Models.Employee
@{
Layout = null;
HttpCookie cookie = Request.Cookies["Detalis"];
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Employee Detail</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div>
<h4>Employee Detail</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.EmployeeID)
</dt>
<dd>
@Html.DisplayFor(model => model.EmployeeID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dd>
@{
String img64 = (string)Session["userImage"];
String img64Url = string.Format("data:image/" +
(string)Session["userImageType"] + ";base64,{0}", img64);
//imagetype can be e.g. gif, jpeg, png etc.
}
<img alt="" src="@img64Url" width="45" height="80"
class="rounded-circle" />
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeID
}) |
@Html.ActionLink("Back to List", "empRecords")
</p>
</body>
</html>
问题
您会收到上述错误,因为 id
是 Employee
控制器中 Details
函数(操作)中的必需参数。
<a href="/Employee/Details">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
解决方案
您必须确保 id
必须包含在 URL 中才能重定向到 Employee/Details
。
URL应该如下所示:
/Employee/Details/{id}
解决方案 1:
<a href="/Employee/Details/@item.EmployeeID">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
解决方案 2:
<a href="@Url.Action("Details", "Employee", new { id = item.EmployeeID})">
@*@Url.Content convert the relative path into application absolute path*@
<img src="@Url.Content("~/Images/" + item.ImageURL)" height="50px" width="50px" />
</a>
补充说明,你也可以让id参数不是必须的,这样就不会给你空指针异常了。在您的代码中,如果您希望 id 参数不能为 null,则需要添加一些验证代码。
public ActionResult Details(long? id = null)
{
// validation code example
if (!id.HasValue)
{
// Redirect or return the appropriate HTTP code here
}
//In case of Invalid user redirect to login
if (Session["login"] == null)
{
return RedirectToAction("Login", "User");
}
EmployeeEntity entity = new EmployeeEntity();
Employee employee = entity.GetSingleEmployee(id);
HttpCookie cookie = Request.Cookies["Detalis"];
if (cookie != null)
{
string color = cookie["Lastlogin"];
}
return View(employee);
}