[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. 跨站请求伪造CSRF

    CSRF是Cross Site Request Forgery的缩写,乍一看和XSS差不多的样子,但是其原理正好相反,XSS是利用合法用户获取其信息,而CSRF是伪造成合法用户发起请求. 在XSS危害 ...

  2. 据说每个大牛、小牛都应该有自己的库——DOM处理续

    在上篇据说每个大牛.小牛都应该有自己的库——DOM处理最后剩下attr()和css()方法没有处理,因为这两个方法当时并不自计划中,是写着写着突然想到的,一时间没有特别好的思路,当时已十一点多了,就去 ...

  3. XCode v8.11 重量级分表分库(无视海量数据)

    XCode天生就有分表分库功能,设计于2005年!历时9年,这是分表分库功能第一次针对性正式更新. 在XCode里面,分表分库非常简单,在操作数据(查询/更新)前修改Meta.ConnName/Met ...

  4. Qt类继承图

    对于学习Qt的,有一个Qt所有类的继承图表,是很有需要的.以下提供了Qt5.1的所有类的继承关系图.看的时候,可以放大和缩小.如需文档下载,可以加入最后的QQ学习群.

  5. EF架构~XMLRepository仓储的实现

    回到目录 对于数据仓储大家应该都很熟悉了,它一般由几个仓储规范和实现它的具体类组成,而仓储的接口与架构本身无关,对于仓储的实现,你可以选择linq2Sql,EF,Nosql,及XML 等等,之前我介绍 ...

  6. lua如何构造类

    function class(super, autoConstructSuper) local classType = {}; classType.autoConstructSuper = autoC ...

  7. Atitit 类库冲突解决方案  httpclient-4.5.2.jar

    Atitit 类库冲突解决方案  httpclient-4.5.2.jar 错误提示如下1 版本如下(client and selenium)2 解决流程2 挂载源码 (SSLConnectionSo ...

  8. 每天一个linux命令(19):find 命令概览

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  9. 1.安装Redis

    首要条件:安装VMware,在虚拟机中安装CentOS. 安装步骤: 1.打开终端(Terminal) 2.在终端输入:wget http://download.redis.io/releases/r ...

  10. Javascript单元测试之QUnit

    首先去Qunit官网下载. Qunit有一个js脚本文件和一个css我们在页面中引入它. <script src="qunit-2.0.1.js"></scrip ...