ASp.MVC HTTP Post 未重定向到操作
ASp.MVC HTTP Post not redirecting to action
我的控制器中有 2 个索引方法,其中一个是 GET,另一个是 POST,用于页面上的表单。
当我提交表单时,当页面刷新时,它通过我的 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");
}
获取方法
[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);
}
您需要return一个结果:
[HttpPost]
public ActionResult Index(ProductViewModel product)
{
// ...
return RedirectToAction("Index");
}
我的控制器中有 2 个索引方法,其中一个是 GET,另一个是 POST,用于页面上的表单。
当我提交表单时,当页面刷新时,它通过我的 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");
}
获取方法
[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);
}
您需要return一个结果:
[HttpPost]
public ActionResult Index(ProductViewModel product)
{
// ...
return RedirectToAction("Index");
}