简体   繁体   English

ASP.NET MVC-管理

[英]ASP.NET MVC - Management

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 that be done without have to create double productController? 问题是,例如“产品”-我有一个“索引”视图来为用户列出产品,但是如果我想制作一个“控制面板”,我应该可以在其中编辑产品(名称,价格,数量),该怎么办? -无需创建双重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 例如,您可以像这样定义ProductController

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文件夹中具有相应的视图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM