Tornike Matiashvili
Tornike Matiashvili

Reputation: 55

Asp.net MVC @RenderPage with Data

I have controller

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);
    }
}

I have a Model

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; }

}

and a simple view

<div class="grid">

@foreach (var item in Model) {

<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>

}

I want to display this data in another page, where I have this code

@RenderPage("~/Views/News/Index.cshtml")

but web page goes on runtime error, with null pointer exception on foreach tag have you any solution with this error? sorry for my english. Hope you understand

Upvotes: 0

Views: 1911

Answers (1)

Aravindan
Aravindan

Reputation: 855

Please use the partial view rendering.

Note main thing you have to mention the namespace in the view page

Like : @model YourApplicationName.Models.exampleClassName

and then render the page as partial view.

 @Html.Partial("partialViewName", new exampleClassName())

or other wise pass the model which you have denoted as namespace in the Partialview like below

@Html.Partial("partialViewName", @Modle.exampleClassName)

or

@Html.Partial("partialViewName", @Modle)

Upvotes: 3

Related Questions