我的控制器中有2个索引方法,其中一个是GET,另一个是POST,用于页面上的表单。
当我提交表单时,当页面刷新时,它会显示一个空白页面而不是运行我的GET索引方法,并且理想情况下会重新加载该页面。
POST METHOD
[HttpPost]
public void Index(ProductViewModel product)
{
try
{
var productContract = Mapper.Map<ProductViewModel, ProductContract>(product);
_productService.CreateProduct(productContract);
}
catch (Exception ex)
{
throw ex;
}
RedirectToAction("Index");
}
获取方法
[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);
}