返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo

简介: 原文:返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo[索引页][源码下载]返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo 作者:webabcd 介绍 以Northwind为示例数据库,使用asp.
原文: 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo

[索引页]
[源码下载]


返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo


作者: webabcd


介绍
以Northwind为示例数据库,使用asp.net mvc 1.0实现添加操作、查询操作、更新操作和删除操作


示例
1、 Model(使用ADO.NET Entity Framework做ORM)
CategorySystem.cs(业务逻辑
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;

namespace  MVC.Models
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
    
/// MVC 之 Model
    
/// Category 业务层逻辑
    
/// </summary>

    public class CategeorySystem
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
{
        
// Northwind 的 ObjectContext
        private NorthwindEntities ctx = new NorthwindEntities();

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 获取 Category 列表
        
/// </summary>
        
/// <returns></returns>

        public List<Categories> GetCategory()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return ctx.Categories.ToList();
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 获取 Category 实体
        
/// </summary>
        
/// <param name="categoryId">类别 ID</param>
        
/// <returns></returns>

        public Categories GetCategory(int categoryId)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return ctx.Categories.FirstOrDefault(p => p.CategoryID == categoryId);
        }

    }

}


ProductSystem.cs( 业务逻辑
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;

namespace  MVC.Models
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
    
/// MVC 之 Model
    
/// Product 业务层逻辑
    
/// </summary>

    public class ProductSystem
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
{
        
// // Northwind 的 ObjectContext
        private NorthwindEntities ctx = new NorthwindEntities();

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 获取产品列表
        
/// </summary>
        
/// <param name="pageIndex">页索引</param>
        
/// <param name="pageSize">页大小</param>
        
/// <returns></returns>

        public List<Products> GetProduct(int pageIndex, int pageSize)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return ctx.Products.OrderBy(p => p.ProductID).Skip(pageIndex * pageSize).Take(pageSize).ToList();
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 获取产品
        
/// </summary>
        
/// <param name="productId">产品 ID</param>
        
/// <returns></returns>

        public Products GetProduct(int productId)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return ctx.Products.FirstOrDefault(p => p.ProductID == productId);
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 新增产品
        
/// </summary>
        
/// <param name="product">产品的 Entity</param>

        public void AddProduct(Products product)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            ctx.AddToProducts(product);
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 删除产品
        
/// </summary>
        
/// <param name="product">产品的 Entity</param>

        public void DeleteProduct(Products product)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            product.Order_Details.Load();
            ctx.DeleteObject(product);
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 在此对象的上下文中保存修改(增/删/改的操作)
        
/// </summary>

        public void Save()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            ctx.SaveChanges();
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 在此对象的上下文中创建 EntityKey
        
/// </summary>
        
/// <param name="entitySetName">实体集的名称</param>
        
/// <param name="entity">实体</param>
        
/// <returns></returns>

        public System.Data.EntityKey CreateEntityKey(string entitySetName, object entity)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return ctx.CreateEntityKey(entitySetName, entity);
        }

    }

}


ValidationEntity.cs(合法性验证)
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;

namespace  MVC.Models
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
    
/// 验证信息的实体
    
/// </summary>

    public class ValidationEntity
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 验证的错误信息
        
/// </summary>

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        public string ErrorMessage getset; }
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 产生错误信息的属性名称
        
/// </summary>

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        public string PropertyName getset; }
        
        
public ValidationEntity(string errorMessage)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            ErrorMessage 
= errorMessage;
        }


        
public ValidationEntity(string errorMessage, string propertyName)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            ErrorMessage 
= errorMessage;
            PropertyName 
= propertyName;
        }

    }

}


Product.cs(合法性验证)
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;

namespace  MVC.Models
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
    
/// 扩展 Product 实体
    
/// 主要是为了对 Product 实体的各个属性做输入的合法性验证
    
/// </summary>

    public partial class Products
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
{
        List
<ValidationEntity> info = new List<ValidationEntity>();

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 对 Product 实体所做的修改是否通过了合法性验证
        
/// </summary>

        public bool IsValid
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
get 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                
return GetValidation().Count() == 0;
            }

        }

        
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 返回验证信息列表
        
/// </summary>
        
/// <returns></returns>

        public List<ValidationEntity> GetValidation()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
return info;
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 重写部分方法 OnProductNameChanging
        
/// 用于在 ProductName 属性改变前,对其做合法性验证
        
/// </summary>
        
/// <param name="value"></param>

        partial void OnProductNameChanging(string value)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
if (string.IsNullOrEmpty(value))
                info.Add(
new ValidationEntity("请输入产品名称""ProductName"));
        }


img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 重写部分方法 OnUnitPriceChanging
        
/// 用于在 UnitPrice 属性改变前,对其做合法性验证
        
/// </summary>
        
/// <param name="value"></param>

        partial void OnUnitPriceChanging(global::System.Nullable<decimal> value)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
if (value == null)
                info.Add(
new ValidationEntity("请输入单价""UnitPrice"));
            
else if (((decimal)value) > 100)
                info.Add(
new ValidationEntity("输入的单价过高""UnitPrice"));
        }

    }

}



2、 Controller
ProductController.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.Mvc;
using  System.Web.Mvc.Ajax;

using  MVC.Models;

namespace  MVC.Controllers
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
    
/// MVC 之 Controller
    
/// 这里体现了 Convention over Configuration
    
/// Controller 类必须以字符串 Controller 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
    
/// 例如 ProductController, Controller 的名称为:Product;其中的 Action 名称有 Index, Details, Edit 等
    
/// </summary>

    public class ProductController : Controller // 需要继承自 System.Web.Mvc.Controller 或者实现 IController 接口
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
    {
        ProductSystem ps 
= new ProductSystem();

        
// Action 的返回值必须为 ActionResult 或 void 

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
        
/// 获取 Product 的列表
        
/// </summary>
        
/// <param name="pageIndex">页索引</param>
        
/// <returns></returns>

        public ActionResult Index(int pageIndex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
int pageSize = 10;
            var products 
= ps.GetProduct(pageIndex, pageSize);

            
// 此 Action 对应的 View 为(按查找的先后顺序) Views/Product/Index.aspx, Views/Product/Index.ascx, Views/Shared/Index.aspx, Views/Shared/Index.ascx
            
// 其所对应的 View 的关联对象为 products
            return View("Index", products);
        }


        
public ActionResult Details(int id)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            var product 
= ps.GetProduct(id);

            
if (product == null)
                
return View("NotFound");
            
else
                
// 对应的 View 的名称默认为 Action 的名称,所以此处所对应的 View 的名称为 Details
                return View(product);
        }


        
public ActionResult Edit(int id)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            var product 
= ps.GetProduct(id);

            
if (product == null)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                
return View("NotFound");
            }

            
else
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                product.CategoriesReference.Load();

                
// 编辑 Product 的时候需要在一个 DropDownList 中选择其所对应的 Category, 所以这里要构造一个名为 CategoryAll 的 ViewData
                
// 因为 Categories 已经是 Product 的属性了,所以这里的 ViewData 的 key 不能为 Categories
                if (product.Categories == null)
                    ViewData[
"CategoryAll"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName");
                
else
                    ViewData[
"CategoryAll"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName", product.Categories.CategoryID);

                
return View("Edit", product);
            }

        }


        
// 可以用 AcceptVerbs 来声明 Action 所对应的 http 方法
        [AcceptVerbs(HttpVerbs.Post)]
        
public ActionResult Edit(int id, FormCollection formValues)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            var product 
= ps.GetProduct(id);

            
// 可以通过这种方式一一为 Product 对象的属性赋值
            
// product.ProductName = Request.Form["ProductName"];

            
// 也可以通过 UpdateModel, 让系统自动为属性赋值(通过反射的方式,取得对象的属性名称,然后和 Request 的 key 做匹配,匹配成功的则赋值)
            UpdateModel<Products>(product);

            var category 
= new CategeorySystem().GetCategory(int.Parse(Request.Form["MyCategory"]));
            product.CategoriesReference.EntityKey 
= ps.CreateEntityKey("Categories", category);

            
// 通过以下的方式让 UpdateModel 只更新指定属性
            
// string[] allowedProperties = new[] { "ProductName", "UnitPrice" };
            
// UpdateModel(product, allowedProperties);

            
if (!product.IsValid)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                
foreach (var validation in product.GetValidation())
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
{
                    
// 设置验证信息
                    ModelState.AddModelError(validation.PropertyName, validation.ErrorMessage);
                }


                
if (product.Categories == null)
                    ViewData[
"CategoryAll"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName");
                
else
                    ViewData[
"CategoryAll"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName", product.Categories.CategoryID);

                
return View(product);
            }


            ps.Save();

            
// 跳转到指定的 Action
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
            return RedirectToAction("Details"new { id = product.ProductID });
        }


        
public ActionResult Create()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            Products product 
= new Products()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                ProductName 
= "请输入产品名称"
            }
;

            
return View(product);
        }


        
// 可以为参数添加声明,如下例:[Bind(Include = "ProductName")],客户端提交的数据中,只有 ProductName 会被绑定到 Product 对象上
        
// [Bind(Include = "ProductName")] 这样的 attribute 也可以声明在类上,用于指定类中需要被绑定的属性
        [AcceptVerbs(HttpVerbs.Post)]
        
public ActionResult Create([Bind(Include = "ProductName")] Products product)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            
if (!product.IsValid)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
{
                
foreach (var issue in product.GetValidation())
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
{
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }


                
return View(product);
            }


            ps.AddProduct(product);
            ps.Save();

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
return RedirectToAction("Details"new { id = product.ProductID });
        }


        
public ActionResult Delete(int id)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            var product 
= ps.GetProduct(id);

            
if (product == null)
                
return View("NotFound");
            
else
                
return View(product);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        
public ActionResult Delete(int id, string confirmButton)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
{
            var product 
= ps.GetProduct(id);

            
if (product == null)
                
return View("NotFound");

            ps.DeleteProduct(product);
            ps.Save();

            
return View("Deleted");
        }

    }

}


3、 View(以列表页为例)
Index.aspx
img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif <% @ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVC.Models.Products>>"  %>

< asp:Content  ID ="Content1"  ContentPlaceHolderID ="TitleContent"  runat ="server" >
    Index
</ asp:Content >
< asp:Content  ID ="Content2"  ContentPlaceHolderID ="MainContent"  runat ="server" >
    
< h2 >
        Index
</ h2 >
    
< table >
        
< tr >
            
< th >
            
</ th >
            
< th >
                ProductID
            
</ th >
            
< th >
                ProductName
            
</ th >
            
< th >
                UnitPrice
            
</ th >
        
</ tr >
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif        
<%   // 因为本页集成了 System.Web.Mvc.ViewPage<IEnumerable<MVC.Models.Products>>
            
// 所以这里的 Model 就是 System.Web.Mvc.ViewPage<IEnumerable<MVC.Models.Products>> 的 Model 属性
            foreach (var item in Model)
            { 
%>
        
< tr >
            
< td >
                
<!-- 这里的 Html 属性类型为 System.Web.Mvc.HtmlHelper -->
                
<% =  Html.ActionLink( " Delete " " Delete " new  { id  =  item.ProductID }) %>
                |
                
<% =  Html.ActionLink( " Edit " " Edit " new  { id  =  item.ProductID })  %>
            
</ td >
            
< td >
                
<% =  Html.ActionLink(item.ProductID.ToString(),  " Details " new  { id = item.ProductID }) %>
            
</ td >
            
< td >
                
<% =  Html.Encode(item.ProductName)  %>
            
</ td >
            
< td >
                
<% =  Html.Encode( String .Format( " {0:F} " , item.UnitPrice))  %>
            
</ td >
        
</ tr >
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif        
<%  }  %>
    
</ table >
    
< p >
        
<% =  Html.RouteLink( " 上一页 " " Products " new  { pageIndex  =  Convert.ToInt32(Html.ViewContext.RouteData.Values[ " pageIndex " ])  -   1  }) %>
        |
        
<% =  Html.RouteLink( " 下一页 " " Products " new  { pageIndex  =  Convert.ToInt32(Html.ViewContext.RouteData.Values[ " pageIndex " ])  +   1  }) %>
    
</ p >
</ asp:Content >


OK
[源码下载]
目录
相关文章
|
1月前
|
开发框架 JavaScript .NET
asp.net中条件查询+分页
asp.net中条件查询+分页
16 1
|
1月前
|
SQL 数据库 C#
C# .NET面试系列十一:数据库SQL查询(附建表语句)
#### 第1题 用一条 SQL 语句 查询出每门课都大于80 分的学生姓名 建表语句: ```sql create table tableA ( name varchar(10), kecheng varchar(10), fenshu int(11) ) DEFAULT CHARSET = 'utf8'; ``` 插入数据 ```sql insert into tableA values ('张三', '语文', 81); insert into tableA values ('张三', '数学', 75); insert into tableA values ('李四',
66 2
C# .NET面试系列十一:数据库SQL查询(附建表语句)
|
1月前
|
SQL 数据库
使用ADO.NET查询和操作数据
使用ADO.NET查询和操作数据
10 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
100 5
|
2月前
|
SQL 开发框架 .NET
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
67 0
|
3月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
25 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
42 0
|
8月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
117 0