开发者

ASP.NET MVC - Management

开发者 https://www.devze.com 2023-01-12 09:34 出处:网络
Hey there, I\'m trying to make a site which have following: News, Products, About and Contact. The problem is that for example the Products - I have an Index view to list the products for the user, bu

Hey there, I'm trying to make a site which have following: News, Products, About and Contact. The problem is that for example the Products - I have an Index view to list the products for the user, but what if I want to make a "control panel" where I should be able to edit the products(names, prices, quantity) - how should t开发者_如何转开发hat be done without have to create double productController?


You can have different views associated to one controller. Each view will be linked to an action method in your controller.

You could, for exemple, define your ProductController class like this

public class ProductController : Controller {
   [HttpGet]
   public ActionResult Index() {
      var productList = ProductService.GetProducts();
      return View( productList );
   }

   [HttpGet]
   public ActionResult Edit( int id ) {
      var product = ProductService.GetProduct( id );
      return View( product );
   }

   [HttpPost]
   public ActionResult Edit( ProductModel product ) {
      if (ModelState.IsValid()) {
         // save the changes
         return RedirectToAction( "Index" );
      }
      return View( product );
   }
}

And have the corresponding views in your Views folder :

Views
| -- Product
    | -- Index.aspx
    | -- Edit.aspx
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号