[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>

mvc jquery 修改 viewbag的更多相关文章

  1. asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发4- 后台模板html页面创建

    上一篇教程<asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发3-登录模块开发>完成了本项目的登录模块,登录后就需要进入后台管理首页了,需要准备一个后台模 ...

  2. asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发2-Model层建立

    上篇(asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发1-准备工作)文章讲解了开发过程中的准备工作,主要创建了项目数据库及项目,本文主要讲解项目M层的实现,M层这里 ...

  3. ECharts 初识(基于MVC+jQuery+Angularjs实现的Demo)

    一.背景:      我们这行做web开发的,很多时候都需要做数据统计报表,现在我所使用的是来自百度团队的ECharts.官方网址:http://echarts.baidu.com/      我们知 ...

  4. jQuery修改class属性和CSS样式

    jQuery修改class属性和CSS样式 class属性修改 类属性即class属性,规定类名. 用类选择器规定样式的时候,需要为元素指定类名,即class属性的值. 注意每个HTML元素只有一个c ...

  5. jquery修改css样式,样式带!important

    由于需求的需要,今天在用jquery修改一个弹出框的样式的时候,由于有一个按钮有padding-left:12px;导致内间距空出来的这一块颜色用普通的方式无法改变. 普通的jquery修改css的方 ...

  6. [转]Spring3 MVC + jQuery easyUI 做的ajax版本用户管理

    原文地址:http://www.iteye.com/topic/1081739 上周写了篇基于spring3.0.5 mvc 简单用户管理实例 ( http://www.iteye.com/topic ...

  7. 使用jquery修改css中带有!important的样式属性

    当CSS中含有!important的样式属性时,普通的修改方式是会出现失败的.如下: <div class="test">使用jquery修改css中带有!import ...

  8. jquery修改a标签的href链接和文字

    可以先体验一下效果:http://keleyi.com/keleyi/phtml/jquery/2.htm 以下修改a标签的href链接和修改文字的代码: <script type=" ...

  9. (转载)MVC + JQUERY + AJAX的几种方式

    MVC + JQUERY + AJAX的几种方式 // 传过去一个简单值,获取一个简单值 $.ajax({            type: "GET",         url: ...

随机推荐

  1. 翻译-高效DevOps的10项实践

    原文链接: http://www.drdobbs.com/architecture-and-design/top-10-practices-for-effective-devops/240149363 ...

  2. 哪些JavaScript IDE最好用?

    阅读本文之前,分享大家一张图片,看图会发现JavaScript开发需求最高,占比达到42.84%,因此掌握JavaScript语言好工作就不愁啦,工欲善其事必先利其器,那么选择IDE来开发是至关重要的 ...

  3. 关于js内部运行机制的一本好书

    读<单页Web应用一书>,第二章讲了js内部运行机制,感觉棒极了.之前读<你不知道的js>,看的云里雾里,似懂非懂.没想到单页Web一书将此内容讲的如此通俗易懂,好多困惑已久的 ...

  4. iOS-性能优化1

      iOS应用是非常注重用户体验的,不光是要求界面设计合理美观,也要求各种UI的反应灵敏,我相信大家对那种一拖就卡卡卡的 TableView 应用没什么好印象.还记得12306么,那个速度,相信大家都 ...

  5. css绘制三角形原理

    1.新建一个元素,将它的宽高都设置为0:然后通过设置border属性来实现三角形效果,下面是css绘制三角形的原理: <!DOCTYPE html> <html> <he ...

  6. ZZUOJ1196: 单调数

    /* 注意的事项:是输出小于 10^n的正整数的个数哦!开始的时候总比样例输出多一个数, 纠结了好久,原来是 0加了进去了! dpI[n][m]表示的是第n位添加数字m(0....9)的构成单调递增数 ...

  7. Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead. Please use Android Tools>Fix project Properties.

    重装操作系统之后,或者破坏了Android的开发环境之后,需要重新配置好Android的开发环境.但是配置好后,导入原有的项目时,报错: Android requires compiler compl ...

  8. [Node.js] Node.js中的流

    原文地址:http://www.moye.me/2015/03/29/streaming_in_node/ 什么是流? 说到流,就涉及到一个*nix的概念:管道——在*nix中,流在Shell中被实现 ...

  9. Codrops 优秀教程:实现效果精美的多层推拉菜单

    Codrops 给我们分享了一个多层菜单的实现教程.他们试图探索创建一个嵌套的多级菜单,是非常有用的东西,可以有很多的内容,如网上商店的导航菜单. 这个 Push Menu 效果理论上可以包含无限嵌套 ...

  10. eclipse推荐的插件

    1.Log4j的颜色插件 http://m.blog.csdn.net/blog/JavaWinner/41548259