mvc jquery 修改 viewbag

  [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;" id="tab">
            <thead>
                <tr>
                    <th colspan="2" align="center" id="header">添加</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>标题:</td>
                    <td>
                        <input name="Title" id="Title" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>作者:</td>
                    <td>
                        <input name="Author" id="Author" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>单价:</td>
                    <td>
                        <input name="Price" id="Price" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>出版社:</td>
                    <td>
                        <select name="PublisherId">
                            @foreach (var item in ViewBag.data as List<Publishers>)
                            {
                               <option value="@item.pid">@item.pubName</option>
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>类型:</td>
                    <td>
                        <select name="CategoryId">
                            @foreach (var item in ViewData["category"] as List<Categories>)
                            {
                               <option value="@item.cid">@item.catName</option>
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>ISBN:</td>
                    <td>
                        <input id="ISBN"  name="ISBN" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>封面:</td>
                    <td>
                        <input id="File1" name="File1" type="file" />
                    </td>
                </tr>
                <tr>
                    <td>点击数:</td>
                    <td>
                        <input id="Clicks" value=" 0" name="Clicks" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>出版日期:</td>
                    <td>
                        <input id="PublishDate" name="publishdate" type="text" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input id="btnOk" type="submit" value="确定" />
                    </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" id="Title" value="@Model.Title" type="text" /></td>
            </tr>
            <tr>
                <td>ISBN:</td>
                <td><input name="ISBN" id="ISBN" value="@Model.ISBN" type="text" /></td>
            </tr>
            <tr>
                <td>出版社:</td>
                <td>
                    <select name="PublisherId">
                        @foreach (var item in ViewBag.data as List<Publishers>)
                        {
                            if(item.pid==Model.PublisherId){
                              <option value="@item.pid" selected>@item.pubName</option>
                            }
                            else
                            {
                                <option value="@item.pid">@item.pubName</option>
                            }
                        }
                    </select>
                </td>
            </tr>
            <tr>
                <td>类型:</td>
                <td>
                    <select name="CategoryId">
                        @foreach (var item in ViewData["category"] as List<Categories>)
                        {
                            if (item.cid == Model.CategoryId)
                            {
                                <option value="@item.cid" selected>@item.catName</option>
                            }
                            else
                            {
                                <option value="@item.cid">@item.catName</option>
                            }
                        }
                    </select>
                </td>
            </tr>
            <tr>
                <td>封面:</td>
                <td>
                    <input name="File1" type="file" /><br />
                    @{string img = Model.ISBN + ".jpg";}
                    <img src="~/BookCovers/@img" width="100" height="150"/>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input id="Submit1" type="submit" value="确定" /></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 id="Button1" type="submit" value="查询" />
                    </form>
                </td>
            </tr>
        </table>
        <table>
            @foreach (var item in Model as List<Books>)
            {
                <tr>
                    <td>@item.Title</td>
                    <td><a href="/Book/Delete/@item.Id">删除</a></td>
                    <td><a href="/Book/Modify/@item.Id">修改</a></td>
                </tr>
            }
        </table>
        <a href="/Book/Add">添加</a>
    </div>
</body>
</html>

posted on 2016-05-30 20:51  DDLL11  阅读(1005)  评论(0编辑  收藏  举报