Fork me on GitHub

InputExtensions 方法解释

http://blog.csdn.net/cnceohjm/article/details/8936669

https://msdn.microsoft.com/zh-cn/library/system.web.mvc.html.inputextensions_methods%28v=vs.118%29.aspx

实体

    public class MovieModels
    {
    }

    public class MovieDB
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Director { get; set; }
        public DateTime Date { get; set; }
    }
/// <add name="MovieDbContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>配置文件中的需要注意name,需要一样不区分大小写
    public class MovieDBContext : DbContext
    {
        public DbSet<MovieDB> Movies { get; set; }
    }

Controller

public class MoviesController : Controller
    {
        private MovieDBContext db = new MovieDBContext();
        //
        // GET: /Movies/

        public ActionResult Index(string searchTitle)//也view名称一样,searchTitle必须与前台查询的名称相同
        {
            var titleQuery = from d in db.Movies orderby d.Title select d.Title;
            ViewBag.SearchTitle = new SelectList(titleQuery);//查询,SearchTitle必须与前台查询的名称相同,否则报错。

            var movies = from m in db.Movies select m;
            if (!string.IsNullOrEmpty(searchTitle))
            {
                return View(movies.Where(x => x.Title == searchTitle));
            }

            return View(db.Movies.ToList());
        }

        public ActionResult Delete(int id = 0)
        {
            MovieDB movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            MovieDB movie = db.Movies.Find(id);
            db.Movies.Remove(movie);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id = 0)
        {
            MovieDB movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }

            return View(movie);
        }

        //
        // GET: /Movies/Create

        public ActionResult Create()//点击create时触发的事件,返回一个空页面
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(MovieDB movie)//保存数据
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(movie);
        }

        //GET:/Movies/Edit/5
        public ActionResult Edit(int id = 0)
        {
            MovieDB movie = db.Movies.Find(id);

            if (movie == null)
            {
                return HttpNotFound();
            }

            return View(movie);
        }

        //POST: /Movies/Edit/5
        [HttpPost]
        public ActionResult Edit(MovieDB movie)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movie).State = System.Data.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(movie);
        }

    }

Index.cshtml

@model IEnumerable<MVC4Movies.Models.MovieDB>//项目名称MVC4Movies
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New","Create")
</p>
<form method="get">
    <p>
        Genre:@Html.DropDownList("SearchTitle", "all")
        <input type="submit" value="search" />
    </p>
</form>
<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.Id)</th>
        <th>@Html.DisplayNameFor(model => model.Title)</th>
        <th>@Html.DisplayNameFor(model => model.Director)</th>
        <th>@Html.DisplayNameFor(model => model.Date)</th>
    </tr>
    @foreach (var item in Model) { 
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Director)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id})|
                @Html.ActionLink("Details", "Details", new { id = item.Id })|
                @Html.ActionLink("Delete", "Delete", new {id = item.Id })
            </td>
        </tr>
    }
</table>

Edit.cshtml

@model MVC4Movies.Models.MovieDB
@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Movie</legend>

        @Html.HiddenFor(model => model.Id)
        
        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Director)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Director)
            @Html.ValidationMessageFor(model => model.Director)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Date)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Date)
            @Html.ValidationMessageFor(model => model.Date)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Details.cshtml

@model MVC4Movies.Models.MovieDB
@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>Movie</legend>

    <div class="display-label">
        @Html.DisplayNameFor(model => model.Title)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>
    <div class="display-label">
        @Html.DisplayNameFor(model => model.Director)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Director)
    </div>
    <div class="display-label">
        @Html.DisplayNameFor(model => model.Date)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Date)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.Id})
    @Html.ActionLink("Back to list", "Index")
</p>

Delete.cshtml

@model MVC4Movies.Models.MovieDB
@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<fieldset>
    <legend>Movie</legend>

    @Html.HiddenFor(model => model.Id)
        
    <div class="display-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>

    <div class="display-label">
        @Html.LabelFor(model => model.Director)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Director)
    </div>

    <div class="display-label">
        @Html.LabelFor(model => model.Date)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Date)
    </div>
</fieldset>

@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}

Create.cshtml

@model MVC4Movies.Models.MovieDB
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Movie</legend>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Director)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Director)
            @Html.ValidationMessageFor(model => model.Director)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Date)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Date)
            @Html.ValidationMessageFor(model => model.Date)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

view下面的文件夹Movies(Index.cshtml.....)

MVC概念

MVC的含义是 “模型-视图-控制器”。MVC是一个架构良好并且易于测试和易于维护的开发模式。基于MVC模式的应用程序包含:

· Models: 表示该应用程序的数据并使用验证逻辑来强制实施业务规则的数据类。

· Views: 应用程序动态生成 HTML所使用的模板文件。

· Controllers: 处理浏览器的请求,取得数据模型,然后指定要响应浏览器请求的视图模板。

本系列教程,我们将覆盖所有这些概念,并告诉您如何使用它们来构建应用程序。

posted on 2014-12-14 23:30  lingfeng95  阅读(919)  评论(0编辑  收藏  举报