Added RedirectUsingNamedRoute
[castle.git] / Samples / Castle / PetStore.Web / Controllers / Admin / ProductManagementController.cs
blobccc114bf17745161304e0033c680f965cc8fa2c0
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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 using System.Collections;
17 namespace PetStore.Web.Controllers.Admin
19 using System;
20 using System.IO;
21 using System.Web;
23 using Castle.MonoRail.Framework;
24 using Castle.MonoRail.Framework.Helpers;
26 using PetStore.Model;
28 /// <summary>
29 /// This controller illustrates a CRUD without
30 /// Scaffolding. Note that this controller does
31 /// not use the service layer (only for the brevity's sake)
32 /// </summary>
33 [Layout("admin")]
34 public class ProductManagementController : AbstractSecureController
36 private String productImagesDir;
38 /// <summary>
39 /// Guess who is going to set this property?
40 /// Yeah, this is inversion of control, baby.
41 /// </summary>
42 public string ProductImagesDir
44 get { return productImagesDir; }
45 set { productImagesDir = value; }
48 public void List()
50 IList products = Product.FindAll();
52 PropertyBag.Add("list", PaginationHelper.CreatePagination(products, 10));
55 public void New()
57 // This is one approach just to reuse the same form
58 // on the edit action and to preserve the inputs
59 // values
60 PropertyBag.Add("product", new Product());
62 // To populate the select html control
63 PropertyBag.Add("categories", Category.FindAll());
65 // Only necessary as we might call this action directly (see below)
66 // from other action
67 RenderView("New");
70 public void Create([DataBind("product")] Product product, HttpPostedFile picture)
72 if (picture.ContentLength == 0)
74 Flash["error"] = "You must attach a picture to the product";
75 New();
76 return;
79 String filename = String.Format("{0}{1}",
80 Guid.NewGuid().ToString("N"), Path.GetExtension(picture.FileName));
82 picture.SaveAs(Path.Combine(GetPictureCompleteDir(), filename));
84 product.PictureFile = filename;
86 product.Save();
88 Redirect("ProductManagement", "list");
91 private String GetPictureCompleteDir()
93 return Path.Combine(Context.ApplicationPhysicalPath, ProductImagesDir);