asp.net - 在 ASP MVC5 中对同一操作定义属性路由后如何重定向到操作

标签 asp.net asp.net-mvc routes 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 空页面, 我们面临的问题是无法找到我们定义的路由,即 [Route("users/{userid}/create")]

http://pastebin.com/WU7XS3Vs

最佳答案

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

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

关于asp.net - 在 ASP MVC5 中对同一操作定义属性路由后如何重定向到操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219393/

相关文章:

asp.net - Asp MenuBar 选中选项卡

c# - 有没有办法将 aspx 文件放入 Visual Studio 2008 .NET 3.5 的类库中?

asp.net-mvc - 如何忽略来自父框的CSS样式?

javascript - Backbone 路由器忽略搜索路由

java - Apache Camel : Splitter, CBR 还是动态路由器?

ASP.NET VB - 从类型 'DBNull' 到类型 'String' 的转换无效

c# - ASP.NET MVC 为编辑和显示模式应用不同的 DisplayFormat

asp.net - 如何在asp.net mvc5中创建动态角色

php - Laravel 5 条件路由和多个 Controller

asp.net - ASP.NET 中的 JQuery 与母版页