首页  ·  知识 ·  编程语言
MVCDemo增删查改
网友  CSDN博客  .NET  编辑:黎昕   图片来源:网络
本实例使用Linqtosql.DB所需要的表:1.Product,2.Category如下图

本实例使用Linq to sql.

DB所需要的表:1.Product,2.Category 如下图

















1.先创建列表页面


















先创建Index 的Control,在Control文件夹下创建ProductControl 文件(在这之前你要使用Linq to sql 创建TestDB文件与DB通信,我这里使用的数据库是Test,并将Product,category 添加到TestDB.dbml文件中)


  1. public class ProductController : Controller  

  2. {  

  3.     //  

  4.     // GET: /Product/  

  5.     //TESTEntities dbConnect = new TESTEntities();  

  6.     TestDBDataContext context = new TestDBDataContext();  

  7.     ProductModelView productView = new ProductModelView();  

  8.     public ActionResult Index(string searchString)  

  9.     {  

  10.         var products = from product in context.Products  

  11.                        select product;  

  12.         if (!string.IsNullOrEmpty(searchString))  

  13.         {  

  14.             products = products.Where(p =>p.ProductName == searchString);  

  15.         }  

  16.         return View(products.ToList());  

  17.     }  

  18. }  




在Model 文件夹中创建ProductModelView文件


  1. publicclass ProductModelView  

  2. {  

  3.     public Product productList { getset; }  

  4. }  



在View文件夹中创建Product文件夹,然后在创建Index.cshtml

@model List<MusicStore.Product>

@{

    ViewBag.Title = "Index";

}

<h2>Index</h2>

@Html.ActionLink("New Create", "Create")

@using (Html.BeginForm("Index","Product","Index"))

{

    <p>ProductName:@Html.TextBox("searchString")

    <input type="submit" value ="search" />

    </p>

}

<table>

<tr>

<td>Id</td>

<td>ProductName</td>

<td>CreateTime</td>

<td>Price</td>

<td></td>

</tr>

@foreach (var item in Model)

{

    <tr>

    <td>@item.Id</td>

    <td>@item.ProductName</td>

    <td>@item.CreateTime</td>

    <td>@item.Price</td>

    <td>

    @Html.ActionLink("Edit", "Edit", new { id = item.Id })

    @Html.ActionLink("Delete", "Delete", new { id = item.Id})

    </td>

    </tr>

}

</table>

此时列表页面已经完成


2.创建添加页面













进入ProductControl文件,添加如下代码


  1. public ActionResult Create()  

  2. {  

  3.     ViewBag.categoryList = GetCategoryitems();  

  4.     return View();  

  5. }  

  6. [HttpPost]  

  7. public ActionResult AddProduct(Product product)  

  8. {  

  9.     context.Products.InsertOnSubmit(product);  

  10.     context.SubmitChanges();  

  11.     return RedirectToAction("Index");  

  12. }  

  13. public List<SelectListItem> GetCategoryitems()  

  14. {  

  15.     var categorys = from category in context.Categories.ToList()  

  16.                     select new SelectListItem()  

  17.                     {  

  18.                         Text = category.CategoryName,  

  19.                         Value = category.Id.ToString()  

  20.                     };  

  21.     List<SelectListItem> items = new List<SelectListItem>();  

  22.     items.Add(new SelectListItem() { Text = "Choose an option" });  

  23.     items.AddRange(categorys);  

  24.     return items;  

  25. }  


然后鼠标放在Create上面创建View 

  1. @model MusicStore.Product  

  2. @{  

  3.     ViewBag.Title = "Create";  

  4. }  

  5. <h2>Create</h2>

  6. @using (Html.BeginForm("AddProduct","Product"))  

  7. {  

  8.     <fieldset>

  9.     <div>

  10.     @Html.Label("Product")  

  11.     </div>

  12.     <div>

  13.     @Html.EditorFor(p =>p.ProductName)  

  14.     </div>

  15.     <div>

  16.     @Html.Label("Price")  

  17.     </div>

  18.     <div>

  19.     @Html.EditorFor(p =>p.Price)  

  20.     </div>

  21.     <div>

  22.     @Html.Label("CreateTime")  

  23.     </div>

  24.     <div>

  25.     @Html.EditorFor(p =>p.CreateTime)  

  26.     </div>

  27.     <div>

  28.     @Html.Label("ProductCategory")  

  29.     </div>

  30.     <div>

  31.     @Html.DropDownListFor(m =>m.ParentId,ViewBag.categoryList as List<SelectListItem>)  

  32.     </div>

  33.     <inputtype="submit"value="Save"/>

  34.     </fieldset>

  35. }  


3.创建编辑界面
















进入ProductControl文件,添加如下代码

  1. public ActionResult Edit(int Id)  

  2. {  

  3.     productView.productList = (from p in context.Products  

  4.                                select p).First(c => c.Id == Id);  

  5.     ViewBag.categoryList = GetCategoryitems();  

  6.     return View(productView);  

  7. }  

  8. [HttpPost]  

  9. public ActionResult Update(ProductModelView productModel)  

  10. {  

  11.     Product product = context.Products.First(p => p.Id == productModel.productList.Id);  

  12.     product.ProductName = productModel.productList.ProductName;  

  13.     product.Price = productModel.productList.Price;  

  14.     product.CreateTime = productModel.productList.CreateTime;  

  15.     product.ParentId = productModel.productList.ParentId;  

  16.     context.SubmitChanges();  

  17.     return RedirectToAction("Index");  

  18. }  


然后添加Edit 的View

  1. @model MusicStore.Models.ProductModelView  

  2. @{  

  3.     ViewBag.Title = "Edit";  

  4. }  

  5. <h2>Edit</h2>

  6. @using (Html.BeginForm("Update", "Product"))  

  7. {  

  8.     <fieldset>

  9.     <legend>Product</legend>

  10.        @Html.HiddenFor(p =>p.productList.Id)  

  11.     <p>

  12.         <div>

  13.     @Html.LabelFor(p => p.productList.ProductName)  

  14.     </div>

  15.     <div>

  16.     @Html.EditorFor(p => p.productList.ProductName)  

  17.     </div>

  18.     </p>

  19. <p>

  20.     <div>

  21.     @Html.LabelFor(p => p.productList.Price)  

  22.     </div>

  23.     <div>

  24.     @Html.EditorFor(p => p.productList.Price)  

  25.     </div>

  26. </p>

  27. <p>

  28.     <div>

  29.     @Html.LabelFor(p => p.productList.CreateTime)  

  30.     </div>

  31.     <div>

  32.     @Html.EditorFor(p => p.productList.CreateTime)  

  33.     </div>

  34.     <div>

  35.     @Html.Label("ProductCategory")  

  36.     </div>

  37.     <div>

  38.     @Html.DropDownListFor(m => m.productList.ParentId, ViewBag.categoryList as List<SelectListItem>)  

  39.     </div>

  40. </p>

  41.     <inputtype="submit"value="submit"/>

  42.     </fieldset>

  43. }  



4.添加删除功能

进入ProductControl添加如下代码即可


  1. public ActionResult Delete(int id)  

  2. {  

  3.     Product produt = context.Products.First(p => p.Id == id);  

  4.     context.Products.DeleteOnSubmit(produt);  

  5.     context.SubmitChanges();  

  6.     return RedirectToAction("Index");  

  7. }  

此 Demo下载地址请点击 here

本文作者:网友 来源:CSDN博客
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的