[HttpGet]
        public ActionResult Modify(int id)
        {
            Books mod=db.Books.Where(b => b.Id == id).FirstOrDefault();
            if (mod != null)
            {
                ViewData["category"] = db.Categories.ToList();
                ViewBag.data = db.Publishers.ToList();
                return View(mod);
            }
            return Content("not found");
        }

        [HttpPost]
        public ActionResult Modify(Books mod)
        {
            HttpPostedFileBase file=Request.Files["File1"];
            if(file!=null)
                file.SaveAs(Server.MapPath("~/BookCovers/"+mod.ISBN+".jpg"));
            Books book=db.Books.Where(b => b.Id == mod.Id).FirstOrDefault();
            book.Title = mod.Title;
            book.ISBN = mod.ISBN;
            db.SaveChanges();
            return Redirect("/Book/Index");
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewData["category"] = db.Categories.ToList();
            ViewBag.data = db.Publishers.ToList();
            return View();
        }
        [HttpPost]
        public ActionResult Add(Books mod)
        {
            HttpPostedFileBase file = Request.Files["File1"];
            if (file != null)
                file.SaveAs(Server.MapPath("~/BookCovers/" + mod.ISBN + ".jpg"));
            mod.PublishDate = DateTime.Now;
            db.Books.Add(mod);
            db.SaveChanges();
            //return Redirect("/Book/Index");
            return RedirectToAction("Index");
        }
        public ActionResult Find()
        {
            string title = Request.Form["title"];
            List<Books> list = null;
            if (title != "")
                list = db.Books.Where(b => b.Title.Contains(title)).ToList();
            else
                list = db.Books.Take(10).ToList();
            return View("index",list);
        }

 

@{
    Layout = null;  //add.cshtml
}
@using MvcApplication2.Models
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <table style="width:400px; margin:0 auto;" />
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

 

@{
    Layout = null;        //modify.cshtml
}
@model MvcApplication2.Models.Books
@using MvcApplication2.Models

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Modify</title>
</head>
<body>
    <form action="/Book/Modify" method="post" enctype="multipart/form-data">
        <input type="hidden" name="Id" value="@Model.Id" />
        <table>
            <tr>
                <td>标题:</td>
                <td><input name="Title" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

 

@{
    Layout = null;    //index.cshtml
}
@using MvcApplication2.Models

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script>
        function find() {
            //location.href = "/Book/Find";
        }
    </script>
</head>
<body>
    <div>
        <table>
            <tr>
                <td>
                    <form action="/Book/Find" method="post">
                        按标题查询:<input name="Title" type="text" /><input >添加</a>
    </div>
</body>
</html>

相关文章: