Asp.net 带数据的 MVC @RenderPage
Asp.net MVC @RenderPage with Data
我有控制器
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(模型中的变量项)
{
<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("~/Views/News/Index.cshtml")
但是网页出现运行时错误,foreach 标签出现空指针异常
你有解决这个错误的方法吗?对不起我的英语不好。希望你明白
请使用局部视图渲染。
请注意您必须在视图页面中提及名称空间的主要事项
喜欢:@model YourApplicationName.Models.exampleClassName
然后将页面呈现为局部视图。
@Html.Partial("partialViewName", new exampleClassName())
或以其他方式传递您在 Partialview 中表示为命名空间的模型,如下所示
@Html.Partial("partialViewName", @Modle.exampleClassName)
或
@Html.Partial("partialViewName", @Modle)
我有控制器
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(模型中的变量项) {
<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("~/Views/News/Index.cshtml")
但是网页出现运行时错误,foreach 标签出现空指针异常 你有解决这个错误的方法吗?对不起我的英语不好。希望你明白
请使用局部视图渲染。
请注意您必须在视图页面中提及名称空间的主要事项
喜欢:@model YourApplicationName.Models.exampleClassName
然后将页面呈现为局部视图。
@Html.Partial("partialViewName", new exampleClassName())
或以其他方式传递您在 Partialview 中表示为命名空间的模型,如下所示
@Html.Partial("partialViewName", @Modle.exampleClassName)
或
@Html.Partial("partialViewName", @Modle)