zoukankan      html  css  js  c++  java
  • MVC 非常有用的代码,终极版

    @using MvcApplication1.Models;
    @model News
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Edit</title>
    </head>
    <body>
        <div>
    
             @using (Html.BeginForm("Update","Home", FormMethod.Post))
                {
            <h1>修改新闻</h1>
            <div>
                @Html.HiddenFor(p=>p.NewsId)
                标题:@Html.TextBoxFor(p => p.Title, new { size=20})<br/>
                作者:@Html.TextBoxFor(p => p.Author, new { size=10 })<br/>
                来源:@Html.TextBoxFor(p=>p.Source, new { size=10 })<br/>
                内容:@Html.TextAreaFor(p=>p.Content, new { cols=30,rows=5})<br/>
                @Html.HiddenFor(p=>p.Time)
                <input name="btnSubmit" type="submit" value="更新" />
                <a href="/Home/Index"><input name="btnReset" type="Button" value="查看"/></a>
            </div>
                }
        </div>
    </body>
    </html>

    Model里的增删改查方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MvcApplication1.Models
    {
        public class NewsBF
        {
            private MyDBDataContext _Context = new MyDBDataContext();
            public List<News> Select()
            {
                return _Context.News.ToList();
            }
            public News Select(int id)
            {
                var query = _Context.News.Where(p=>p.NewsId == id);
                if (query.Count() > 0)
                {
                    return query.First();
                }
                return null;
            }
            public void Insert(News data)
            {
                _Context.News.InsertOnSubmit(data);
                _Context.SubmitChanges();
            }
            public void Update(News data)
            {
                var query = _Context.News.Where(p=>p.NewsId == data.NewsId);
                if(query.Count() > 0)
                {
                    News news = query.First();
                    news.Title = data.Title;
                    news.Author = data.Author;
                    news.Content = data.Content;
                    news.Source = data.Source;
                    news.Time =data.Time;
                    _Context.SubmitChanges();
                }
            }
            public void Delete(int newsID)
            {
                var query = _Context.News.Where(p=>p.NewsId == newsID);
                if(query.Count() > 0)
                {
                    News news = query.First();
                    _Context.News.DeleteOnSubmit(news);
                    _Context.SubmitChanges();
                }
            }
        }
    
    }


    控制力的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcApplication1.Models;
    namespace MvcApplication1.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Default1/
    
            public ActionResult Index()
            {
                List<News> list = new NewsBF().Select();
    
                return View(list);
            }
            public ActionResult Delete(string id)
            {
                try
                {
                    int newsID = Convert.ToInt32(id);
                    new NewsBF().Delete(newsID);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return RedirectToAction("Index", "Error");
                }
            }
            public ActionResult Add()
            {
                News data = new News();
                return View(data);
            }
            public ActionResult Insert(News data)
            {
                try
                {
                    data.Time = DateTime.Now;
                    new NewsBF().Insert(data); //送到数据库中去
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return RedirectToAction("Index", "Error");
                }
            }
            public ActionResult Edit(string id)
            {
                int newsID = 0;
                try
                {
                    newsID = Convert.ToInt32(id);
                    News data = new NewsBF().Select(newsID);
                    if (data != null)
                    {
                        return View(data);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Error");
                    }
                }
                catch
                {
                    return RedirectToAction("Index", "Error");
                }
            }
            public ActionResult Update(News data)
            {
                try
                {
                    //data.Time = DateTime.Now;
                    new NewsBF().Update(data);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return RedirectToAction("Index", "Error");
                }
            }
        }
    }

    视图里的代码

    @using MvcApplication5xinwen.Controllers;
    @using MvcApplication5xinwen.Models;
    @model List<news>
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Xianshi</title>
    </head>
         <style>
            .tr1 
            {
             background-color:blue;
             text-align:center;
             font-weight:bold;
            }
            .tr2
            {
                background-color:#FFFFCC;
                text-align:center;
            }
        </style>
    <body>
        <div>
            <table width="100%" cellpadding="4" cellspacing="1" border="1">
                <tr class="tr1">
                    <td>编号</td>
                    <td>标题</td> 
                    <td>作者</td>
                    <td>来源</td>
                    <td>内容</td>
                    <td>时间</td>
                    <td>操作</td>
                </tr>
                @{foreach(news data in Model)
                  {
                <tr class="tr2">
                    <td>@data.newsid</td>
                    <td>@data.title</td>
                    <td>@data.author</td>
                    <td>@data.source</td>
                    <td>@data.content</td>
                    <td>@data.times</td>
                    <td>
                        <a href="/Home/Exit/ @data.newsid">修改</a>
                        <a href="/Home/Delete/@data.newsid " onclick="return confirm('确认要删除这条新闻吗?')">删除</a>
                    </td>
    
                </tr>
                }
                }
            </table>
        </div>
    </body>
    </html>
    @using MvcApplication1.Models;
    @model MvcApplication1.Models.News
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Add</title>
    </head>
    <body>
        <div>
    
             @using (Html.BeginForm("Insert","Home", FormMethod.Post))
                {
            <h1>发布新闻</h1>
            <div>
                标题:@Html.TextBoxFor(p => p.Title, new { size=20})<br/>
                作者:@Html.TextBoxFor(p => p.Author, new { size=10 })<br/>
                来源:@Html.TextBoxFor(p=>p.Source, new { size=10 })<br/>
                内容:@Html.TextAreaFor(p=>p.Content, new { cols=30,rows=5})<br/>
                <input name="btnSubmit" type="submit" value="发布" />
                <a href="/Home/Index"><input name="btnReset" type="Button" value="查看"/></a>
            </div>
                }
        </div>
    </body>
    </html>
  • 相关阅读:
    【五月每日总结】
    【Codeforces Round #405 ( Div 2)】题解
    【Codeforces Round #406 (Div. 2)】题解
    【HUD-5790】Prefix (主席树+tire)
    【倍增】LCM QUERY
    【hackerrank】Week of Code 30
    【Codeforces Round #404 (Div. 2)】题解
    【数论】逆元
    mongodb复制集部署文档
    合并SQL 调优
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4633310.html
Copyright © 2011-2022 走看看