我有控制器
public class NewsController : Controller
{
private SchoolDbContext db = new SchoolDbContext();
//
// GET: /News/
public ActionResult Index()
{
return View(db.News.ToList());
}
//
// GET: /News/Details/5
public ActionResult Details(int id = 0)
{
News news = db.News.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
//
// GET: /News/Create
public ActionResult Create()
{
return View();
}
//
// POST: /News/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(News news)
{
if (ModelState.IsValid)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
string path2 = Path.GetRandomFileName();
fileName = path2 + fileName;
var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
news.Image = fileName;
file.SaveAs(path);
}
db.News.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
//
// GET: /News/Edit/5
public ActionResult Edit(int id = 0)
{
News news = db.News.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
//
// POST: /News/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(News news)
{
if (ModelState.IsValid)
{
db.Entry(news).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
//
// GET: /News/Delete/5
public ActionResult Delete(int id = 0)
{
News news = db.News.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
//
// POST: /News/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
News news = db.News.Find(id);
db.News.Remove(news);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
我有一个模特
public class News
{
[Key]
public int newsID { get; set; }
[Required]
public string newsName { get; set; }
[Required]
public string newsDescription { get; set; }
public string Image { get; set; }
}
和一个简单的视图
<div class="grid">
@foreach(模型中的var项) {
<div class="holder_content">
<section class="group1">
<h3>@Html.DisplayFor(modelItem => item.newsName)</h3>
<p class="desc">@Html.DisplayFor(modelItem => item.newsDescription)</p>
<a class="photo_hover3" href="#"><img src="~/Uploads/@Html.DisplayFor(modelItem => item.Image)" width="240" height="214" alt=""></a>
<div class="forbutton">
@Html.ActionLink("სრულად ", "Details", new { id = item.newsID }, new { @class = "button" })
</div>
@{ if (User.Identity.IsAuthenticated)
{
@Html.ActionLink("Edit ", "Edit", new { id = item.newsID })
@Html.ActionLink("Delete", "Delete", new { id = item.newsID })
}
}
</section>
}
我想在另一个页面中显示这些数据,我有这个代码
@RenderPage(&#34;〜/查看/新闻/ Index.cshtml&#34)
但网页继续运行时出现错误,foreach标记上出现空指针异常 你有这个错误的解决方案吗?对不起我的英语不好。希望你理解
答案 0 :(得分:3)
请使用局部视图渲染。
请注意,您必须在视图页面中提及命名空间
赞:@model YourApplicationName.Models.exampleClassName
然后将页面呈现为部分视图。
@Html.Partial("partialViewName", new exampleClassName())
或以其他方式传递您在Partialview中表示为名称空间的模型,如下所示
@Html.Partial("partialViewName", @Modle.exampleClassName)
或
@Html.Partial("partialViewName", @Modle)