当前位置: 主页 > 编程知识 > net编程 > ASP.NET应用: 在MVC中如何进行表的增删改操作

ASP.NET应用: 在MVC中如何进行表的增删改操作

时间:2009-12-10来源:站长资讯网 点击:

三、通用增加、修改

首先添加一个CreateEditView.aspx视图

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    
<%Html.RenderPartial(ViewData["PartialName"].ToString()); %>

</asp:Content>

然后添加两个Partial视图News.ascx和User.ascx,这两个视图是分别基于News和User类的强类型视图,具体内容参加源码。

接下来我们添加相应的Controller

 public ActionResult CreateEditView(string partialName, int? key)
        {

            ViewData[
"PartialName"= partialName;

            RepositoryBase repositoryBase 
= new RepositoryBase(partialName);
            ICommonTable table;
            
if (key == null)
            {
                table 
= repositoryBase.CreateNew();
            }
            
else
            {
                table 
= repositoryBase.Get(key ?? 0);
            }

            
return View("CreateEditView", table);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        
public ActionResult CreateEditView(string partialName, int? key, FormCollection formCollection)
        {
            RepositoryBase repositoryBase 
= new RepositoryBase(partialName);
            ICommonTable bllTable;
            
if (key == null)
            {
                bllTable 
= repositoryBase.CreateNew();
            }
            
else
            {
                bllTable 
= repositoryBase.Get(key ?? 0);
            }

            
this.UpdateModel(bllTable, true);
            
if (key == null)
            {
                Context.GetContext().GetTable(repositoryBase.EntityType).InsertOnSubmit(bllTable);

            }

            Context.GetContext().SubmitChanges();


            
return RedirectToAction(partialName+"List");//返回到list
        }

这里边大家可能有疑问的就是this.UpdateModel(bllTable, true);这个方法在mvc框架中并不存在,这是我添加的扩展方法,这个地方如果使用UpdateModel(bllTable)虽然编译不会报错,但也没有更新成功,查了一下mvc的源码,问题就出在如下源码中:

 protected internal bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IDictionary<string, ValueProviderResult> valueProvider) where TModel : class {
            
if (model == null) {
                
throw new ArgumentNullException("model");
            }
            
if (valueProvider == null) {
                
throw new ArgumentNullException("valueProvider");
            }

            Predicate
<string> propertyFilter = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties);
            IModelBinder binder 
= Binders.GetBinder(typeof(TModel));

            ModelBindingContext bindingContext 
= new ModelBindingContext() {
                Model 
= model,
                ModelName 
= prefix,
                ModelState 
= ModelState,
                ModelType 
= typeof(TModel),
                PropertyFilter 
= propertyFilter,
                ValueProvider 
= valueProvider
            };
            binder.BindModel(ControllerContext, bindingContext);
            
return ModelState.IsValid;
        }

站长资讯网
.
分页: [1] [2] [3] [4]
TAG: ASP.NET MVC
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言