在ASP.NET MVC 5中添加文章并设置文章属性,通常涉及以下几个步骤:创建模型、控制器、视图以及配置路由,以下是详细的步骤和代码示例:
### 1. 创建模型
我们需要创建一个表示文章的模型类,假设我们的文章有标题、内容和发布日期三个属性。
“`csharp
public class Article
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PublishDate { get; set; }
“`
### 2. 创建数据库上下文
我们需要创建一个数据库上下文类来管理我们的数据库操作。
“`csharp
using System.Data.Entity;
public class BlogContext : DbContext
public DbSet
“`
### 3. 创建控制器
我们需要创建一个控制器来处理文章的添加和显示。
“`csharp
using System.Linq;
using System.Web.Mvc;
public class ArticlesController : Controller
private BlogContext db = new BlogContext();
// GET: Articles/Create
public ActionResult Create()
{
return View();
}
// POST: Articles/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = “Id,Title,Content,PublishDate”)] Article article)
{
if (ModelState.IsValid)
{
db.Articles.Add(article);
db.SaveChanges();
return RedirectToAction(“Index”);
}
return View(article);
}
// GET: Articles
public ActionResult Index()
{
var articles = db.Articles.Include(a => a).ToList();
return View(articles);
}
“`
### 4. 创建视图
我们需要为创建文章和显示文章列表创建视图。
#### Create视图(Create.cshtml)
“`html
@model YourNamespace.Models.Article
@{
ViewBag.Title = “Create”;
Create
@using (Html.BeginForm())
@Html.AntiForgeryToken()
Article
@Html.ValidationSummary(true, “”, new { @class = “textdanger” })
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = “controllabel colmd2” })
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = “formcontrol” } })
@Html.ValidationMessageFor(model => model.Title, “”, new { @class = “textdanger” })
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = “controllabel colmd2” })
@Html.TextAreaFor(model => model.Content, new { htmlAttributes = new { @class = “formcontrol” } })
@Html.ValidationMessageFor(model => model.Content, “”, new { @class = “textdanger” })
@Html.LabelFor(model => model.PublishDate, htmlAttributes: new { @class = “controllabel colmd2” })
@Html.EditorFor(model => model.PublishDate, new { htmlAttributes = new { @class = “formcontrol” } })
@Html.ValidationMessageFor(model => model.PublishDate, “”, new { @class = “textdanger” })
“`
#### Index视图(Index.cshtml)
“`html
@model IEnumerable
@{
ViewBag.Title = “Index”;
Index
@Html.ActionLink(“Create New”, “Create”)
@Html.DisplayNameFor(model => model.Title) | @Html.DisplayNameFor(model => model.Content) | @Html.DisplayNameFor(model => model.PublishDate) | |
---|---|---|---|
@Html.DisplayFor(modelItem => item.Title) | @Html.DisplayFor(modelItem => item.Content) | @Html.DisplayFor(modelItem => item.PublishDate) | @Html.ActionLink(“Edit”, “Edit”, new { id=item.Id }) | @Html.ActionLink(“Details”, “Details”, new { id=item.Id }) | @Html.ActionLink(“Delete”, “Delete”, new { id=item.Id }) |
“`
### 5. 配置路由
确保你的路由配置正确,默认情况下,MVC 5已经配置好了基本的路由,但你可以检查`RouteConfig.cs`文件以确保它包含以下内容:
“`csharp
public class RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
“`
通过以上步骤,你应该能够在ASP.NET MVC 5中成功添加文章并设置其属性。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1230988.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复