zoukankan      html  css  js  c++  java
  • MVC+EF实现增删查改

    一、Index主页(查询)

    HomeController

    MVCDemoEntities entity = new MVCDemoEntities();

    public ActionResult Index()
    {
    var u = entity.Users.ToList();      ToList()是必备的,养成好这个习惯
    return View(u);
    }

    Index.cshtml

    @model IEnumerable<MVCDemo.Models.User>

    @{
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    </head>
    <body>
    <div>
    @Html.ActionLink("新增", "Add")
    <table>
    <tr>
    <th>Id</th>
    <th>UserName</th>
    <th>Age</th>
    <th>Edit</th>
    <th>Delete</th>
    </tr>
    @foreach (var u in Model)
    {

    <tr>
    <td>@u.Id</td>
    <td>@u.UserName</td>
    <td>@u.Age</td>
    <td>@Html.ActionLink("编辑", "Edit", new { id=u.Id})</td>
    <td>@Html.ActionLink("删除", "Delete", new { id=u.Id})</td>      Id作为参数传递,便于删除和修改时候的查询
    </tr>
    }

    </table>
    </div>
    </body>
    </html>

    二、新增

    HomeController

    public ActionResult Add()
    {

    return View();
    }
    [HttpPost]
    public ActionResult Add(User u)
    {
    User u1 = new User();
    u1.UserName=Request["UserName"];    从cshtml页面中获取值
    u1.Age = Convert.ToInt32(Request["Age"]);
    entity.Users.Add(u1);
    entity.SaveChanges();

    return RedirectToAction("Index");
    }

    Add.CShtml(使用的是模板)

    @using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
    <legend>User</legend>

    <div class="editor-label">
    @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.UserName)
    @Html.ValidationMessageFor(model => model.UserName)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Age)
    @Html.ValidationMessageFor(model => model.Age)
    </div>

    <p>
    <input type="submit" value="Create" />
    </p>
    </fieldset>
    }

    三、修改

    HomeController

    [HttpGet]
    public ActionResult Edit(int id)
    {
    //根据id查找对应的数据 然后展示
    User user = entity.Users.FirstOrDefault(u => u.Id == id);

    return View(user);
    }
    [HttpPost]
    public ActionResult Edit()
    {
    int id =Convert.ToInt32( Request["userId"]);
    User user = entity.Users.FirstOrDefault(u => u.Id == id);
    user.UserName = Request["UserName"];
    user.Age = Convert.ToInt32(Request["Age"]);
    entity.SaveChanges();
    return RedirectToAction("Index");
    }

    Edit.cshtml

    <div>
    @using (Html.BeginForm())
    {
    <table>
    <tr>
    <td>
    @Html.LabelFor(model => model.UserName)
    </td>
    <td>
    @Html.TextBox("UserName",Model.UserName)
    </td>
    </tr>
    <tr>
    <td>
    @Html.LabelFor(model => model.Age)
    </td>
    <td>
    @Html.TextBox("Age",Model.Age)
    </td>
    </tr>
    <input type="hidden" name="userId" value="@Model.Id" />用隐藏的标签来存储Id
    <input type="submit" name="name" value="提交" />
    </table>
    }

    四、删除

    HomeController

    public ActionResult Delete(int id)
    {
    User user = entity.Users.FirstOrDefault(u => u.Id == id);
    entity.Users.Remove(user);
    entity.SaveChanges();
    return RedirectToAction("Index");
    }

     

  • 相关阅读:
    编程题目----约德尔测试
    编程题目----路灯
    java----java垃圾回收算法
    JAVA多线程和并发基础面试问答(转载)
    科学史上非常伟大的十位单身科学家
    MySQL----mysql57服务突然不见了的,解决方法
    linux:644、755、777权限详解
    Java 关键字 速查表
    java----鲁棒性
    习题----第六章 图(转)
  • 原文地址:https://www.cnblogs.com/niyingying/p/5292238.html
Copyright © 2011-2022 走看看