2016-04-08 37 views
1

我在我的控制器中有2個索引方法,其中一個是GET,另一個是在頁面上的窗體上使用的POST。ASp.MVC HTTP Post not notirecting to action

當我提交表單時,當頁面刷新時,它顯示一個空白頁面,而不是通過我的GET索引方法運行並理想地重新加載該頁面。

POST方法

[HttpPost] 
    public void Index(ProductViewModel product) 
    { 
     try 
     { 
      var productContract = Mapper.Map<ProductViewModel, ProductContract>(product); 
      _productService.CreateProduct(productContract); 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 
     RedirectToAction("Index"); 
    } 

GET方法

[HttpGet] 
    public ViewResult Index() 
    { 
     _productService = new ProductServiceClient(); 
     var brandSerice = new ProductBrandServiceClient(); 
     var categoryService = new ProductCategoryServiceClient(); 

     var productPageViewModel = new ProductPageViewModel(); 
     var productViewModelList = new List<ProductViewModel>(); 
     var productBrandsViewModelList = new List<ProductBrandViewModel>(); 
     var productCategoriesViewModelList = new List<ProductCategoryViewModel>(); 

     try 
     { 
      productViewModelList.AddRange(_productService.GetProducts().Select(Mapper.Map<ProductContract, ProductViewModel>)); 
      productBrandsViewModelList.AddRange(brandSerice.GetProductBrands().Select(Mapper.Map<ProductBrandContract, ProductBrandViewModel>)); 
      productCategoriesViewModelList.AddRange(categoryService.GetProductCategories().Select(Mapper.Map<ProductCategoryContract, ProductCategoryViewModel>)); 

      productPageViewModel.ProductList = productViewModelList; 

      productPageViewModel.ProductBrands = new SelectList(productBrandsViewModelList, "Id", "Description"); 
      productPageViewModel.ProductCategories = new SelectList(productCategoriesViewModelList, "Id", "Description"); 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     return View(productPageViewModel); 
    } 
+2

注意:您'catch'塊不僅是多餘的,它們是有害的。他們應該完全被刪除。 – David

+0

你已經發表了一個聲明,並沒有真正解釋爲什麼,這將是什麼原因@David –

+0

他們是多餘的,因爲他們實際上沒有做任何事情。它們是有害的,因爲它們拋棄了堆棧跟蹤信息並將其替換爲新的堆棧跟蹤,從而有效地隱藏實際異常發生的位置。 – David

回答

2

你需要返回一個結果:

[HttpPost] 
public ActionResult Index(ProductViewModel product) 
{ 
    // ... 

    return RedirectToAction("Index"); 
}