DropDownListFor from ASP.NET MVC Projects
Здравейте,
При създаване на категории браузъра ми отпечатва грешка в DropDownListFor във файла Create .
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
|
Все още не съм написал нищо в Article.cs в папката с моделите защото при изписване на properties всичко се чупи.
@model Blog.Models.ArticleViewModel
@{
ViewBag.Title = "Create";
}
<div class="container">
<div class="well">
<h2>Create Article</h2>
@using (Html.BeginForm("Create", "Article", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Title, new { @class = "control-label col-sm-4" })
<div class="col-sm-4">
@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Content, new { @class = "control-label col-sm-4" })
<div class="dropdown col-sm-4">
@Html.DropDownListFor(m => m.CategoryId,
new SelectList(Model.Categories, "Id", "Name"))
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.CategoryId, new { @class = "control-label col-sm-4" })
<div class="col-sm-4">
@Html.TextAreaFor(m => m.Categories, new { @class = "form-control", @rows = "7" })
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-4">
@Html.ActionLink("Cancel", "Index", "Article", null, new { @class = "btn btn-default" })
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
}
</div>
</div>
Това е Create от папка View/ Article .Иначе в View/Categories да не е инициализирано Details.
Дай кода от контролера.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Blog.Models;
namespace Blog.Controllers.Admin
{
public class CategoriesController : Controller
{
private BlogDbContext db = new BlogDbContext();
// GET: Categories
public ActionResult Index()
{
return View(db.Categories.ToList());
}
// GET: Categories/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// GET: Categories/Create
public ActionResult Create()
{
return View();
}
// POST: Categories/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name")] Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
// GET: Categories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: Categories/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name")] Category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
// GET: Categories/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Categories.Find(id);
db.Categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}