1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.ActiveRecordSupport
.Scaffold
18 using System
.Collections
;
19 using System
.Reflection
;
20 using Castle
.ActiveRecord
;
21 using Castle
.Components
.Common
.TemplateEngine
;
22 using Castle
.MonoRail
.Framework
;
25 /// Performs the update
28 /// Searchs for a template named <c>create{name}</c>
30 public class UpdateAction
: AbstractScaffoldAction
32 public UpdateAction(Type modelType
, ITemplateEngine templateEngine
, bool useModelName
, bool useDefaultLayout
) :
33 base(modelType
, templateEngine
, useModelName
, useDefaultLayout
)
37 protected override string ComputeTemplateName(IControllerContext controller
)
39 return controller
.Name
+ "\\update{1}";
42 protected override void PerformActionProcess(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
44 object instance
= null;
48 AssertIsPost(engineContext
.Request
.HttpMethod
);
50 instance
= binder
.BindObject(Model
.Type
, Model
.Type
.Name
, builder
.BuildSourceNode(engineContext
.Request
.Form
));
52 CommonOperationUtils
.SaveInstance(instance
, controller
, errors
, ref prop2Validation
, false);
54 SessionScope
.Current
.Flush();
58 engineContext
.Response
.RedirectUsingRoute(
59 controllerContext
.AreaName
, controllerContext
.Name
, "list" + Model
.Type
.Name
, true);
63 engineContext
.Response
.RedirectUsingRoute(
64 controllerContext
.AreaName
, controllerContext
.Name
, "list", true);
69 errors
.Add("Could not save " + Model
.Type
.Name
+ ". " + ex
.Message
);
72 if (errors
.Count
!= 0)
74 engineContext
.Flash
[Model
.Type
.Name
] = instance
;
75 engineContext
.Flash
["errors"] = errors
;
77 PropertyInfo keyProp
= ObtainPKProperty();
78 IDictionary props
= new Hashtable();
82 props
[keyProp
.Name
] = keyProp
.GetValue(instance
, null);
87 engineContext
.Response
.Redirect(controllerContext
.AreaName
, controllerContext
.Name
, "edit" + Model
.Type
.Name
, props
);
91 engineContext
.Response
.Redirect(controllerContext
.AreaName
, controllerContext
.Name
, "edit", props
);
96 protected override void RenderStandardHtml(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)