星空网 > 软件开发 > ASP.net

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 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='/images/loading.gif' data-original="~/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="http://www.cnblogs.com//Book/Delete/@item.Id">删除</a></td>
                    <td><a href="http://www.cnblogs.com//Book/Modify/@item.Id">修改</a></td>
                </tr>
            }
        </table>
        <a href="http://www.cnblogs.com//Book/Add">添加</a>
    </div>
</body>
</html>




原标题:mvc jquery 修改 viewbag

关键词:jquery

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

四大跨境物流:https://www.goluckyvip.com/tag/92833.html
跨境物流数字化:https://www.goluckyvip.com/tag/92834.html
数字化跨境物流:https://www.goluckyvip.com/tag/92835.html
云速跨境物流:https://www.goluckyvip.com/tag/92836.html
跨境法国物流:https://www.goluckyvip.com/tag/92837.html
法国跨境物流:https://www.goluckyvip.com/tag/92838.html
下周开始,安徽人免费游九华山,带上身份证就:https://www.vstour.cn/a/408234.html
上海滑雪场门票价格?:https://www.vstour.cn/a/408235.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流