如何在ASP MVC5中的同一操作上定义属性路由后重定向到操作

时间:2016-02-05 08:25:00

标签: asp.net asp.net-mvc routing attributes asp.net-mvc-routing

以下是代码

   [Route("users/{userid}/create")]
   public ActionResult CreateSellerProfile(string userId)
   {
       var country = _locationService.GetCountries().ToDictionary(x => x.Id, x => x.CountryName);
       var model = new SellerProfileViewModel { CountryDic = country };
       model.UserId = userId;
       return View(model);
   }
   [Authorize]

   [HttpPost]

   [Route("users/create")]
   public ActionResult CreateSellerProfile(SellerProfileViewModel model)
   {
     if (!ModelState.IsValid)
       {
           model.StateDic = _locationService.Getstates(model.CountryId).ToDictionary(x => x.Id, x => x.StateName);

           model.CountryDic = _locationService.GetCountries().ToDictionary(x => x.Id, x => x.CountryName);
           return View(model);
       }



       var checkForalreadyExists = _userService.GetSellerProfileByUserId(model.UserId);
       if (checkForalreadyExists == null)
       {
           var domainSellerObj = Mapper.Map<SellerProfileViewModel, SellerProfile>(model);
           _userService.SaveSellerProfile(domainSellerObj);

         return RedirectToAction("CreateSellerProfile", new { model.UserId });

       }
       else
       {
           SetMessage(MessageType.Danger, MessageConstant.GetMessage(Messages.SellerProfileAdded));

     return RedirectToAction("CreateSellerProfile", new { model.UserId });

       }
   }

我想问一下,在我们对这个动作CreateSellerProfile进行POST之后, 然后我们想再次重定向到同一个动作CreateSellerProfile空页面, 我们面临的问题是它无法找到我们定义的路线,即[路线(&#34;用户/ {用户ID} /创建&#34;)]

http://pastebin.com/WU7XS3Vs

1 个答案:

答案 0 :(得分:0)

您需要在匿名对象中指定参数名称(userId):

return RedirectToAction("CreateSellerProfile", new { userId = model.UserId });