Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC在每个页面上重构[httpPost]处理程序?_Asp.net Mvc 3_Forms_Refactoring - Fatal编程技术网

Asp.net mvc 3 MVC在每个页面上重构[httpPost]处理程序?

Asp.net mvc 3 MVC在每个页面上重构[httpPost]处理程序?,asp.net-mvc-3,forms,refactoring,Asp.net Mvc 3,Forms,Refactoring,我在我的站点的每个页面上都有一个简单的表单,表单返回并由页面控制器处理 ForController在每一页上 [HttpPost] public ActionResult Index(Models.FindTutorFormModel formModel) { if (ModelState.IsValid) { return Redirect("/thanks/"); } return View(); } 我真的不想为站点中的每个页面控制器复制代码

我在我的站点的每个页面上都有一个简单的表单,表单返回并由页面控制器处理

ForController在每一页上

 [HttpPost]
 public ActionResult Index(Models.FindTutorFormModel formModel)
 {
   if (ModelState.IsValid)
   {
      return Redirect("/thanks/");
   }
   return View();
  }

我真的不想为站点中的每个页面控制器复制代码。有没有简单的方法来重构它?

我不确定这是否是执行任务的最佳方法,但您可以使用继承

    using System.Web.Mvc;

    namespace MvcApplication2.Controllers
    {
        public class BaseController : Controller
        {
            public ActionResult Thanks()
            {
                return Content("Thanks!");
            }
        }

        public class HomeController : BaseController
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";
                return View();
            }
            public ActionResult About()
            {
                return View();
            }
        }

        public class InfoController : BaseController
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";
                return View();
            }
        }
    }
因此,在每个控制器中,都有相同的方法。
我希望有帮助

你为什么要删除另一个问题?我有一个有效的解决方案给你。这可以通过局部视图来处理。查看我在你的另一个问题上发布的答案对不起,重读了几遍,觉得我问错了。这个问题也差不多,我认为这个问题写得很好,所有细节都很相关。你应该把它留给将来有相同场景的读者。