简体   繁体   English

使用ASP.NET MVC将ActionResults创建和编辑合并为一个

[英]Merging Create and Edit ActionResults into one using ASP.NET MVC

I currently have 4 action methods: 我目前有4种动作方法:

UserCreate - Get UserCreate - 获取
UserCreate - Post UserCreate - 发布

UserEdit - Get UserEdit - 获取
UserEdit - Post UserEdit - 发布

My goal is to somehow combine them do just be UserEdit. 我的目标是以某种方式将它们组合起来只做UserEdit。 The only difference between the two is that UserCreate asks for Password and ConfirmPassword in the form. 两者之间的唯一区别是UserCreate在表单中要求输入Password和ConfirmPassword。 UserEdit does not. UserEdit没有。 I ended up having to create 2 separate ViewModels for this: UserCreateViewModel, UserEditViewModel. 我最终必须为此创建2个单独的ViewModel:UserCreateViewModel,UserEditViewModel。

// GET: Users/Create
[Route("users/create")]
public IActionResult UserCreate()
{
    return View();
}

// POST: Users/Create
[Route("users/create")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UserCreate(UserCreateViewModel vm)
{        
}

// GET: Users/Edit/5
[Route("users/edit/{id}")]
public async Task<IActionResult> UserEdit(string id)
{
    return View(vm);
}

// POST: Users/Edit/5
[Route("users/edit/{id}")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UserEdit(UserEditViewModel vm)
{
    return View(vm);
}

If keeping them separate is better and more MVC convention that is fine, but I just want to make sure there isn't some feature of MVC that I am not utilizing that can help me have only 2 Action methods, One view model for this. 如果将它们保持分离是更好的,更好的MVC约定,但我只是想确保没有我没有使用的MVC的某些功能可以帮助我只有2个Action方法,一个视图模型。

Keep them separate. 将它们分开。 You aren't missing anything in MVC that is meant for working 'around' this so to say. 你不会错过MVC中任何可以解决这个问题的事情。 Sure you could make it work but it strays from common api implementation for the sake of thinking they are similar actions. 当然你可以使它工作但它偏离了常见的api实现,以便认为它们是类似的行为。 If it feels really bad you could use the same view model and code validations into page and server side (you couldn't rely on it being on the model as validation would now require two scenarios for one model) but if also split it out into two. 如果感觉非常糟糕,你可以在页面和服务器端使用相同的视图模型和代码验证(你不能依赖它在模型上,因为验证现在需要两个场景用于一个模型)但是如果它也分成了二。 It's cleaner. 它更清洁。

Yes you can certainly have only 2methods using mvc feature: 是的,你肯定只有2个使用mvc功能的方法:

You can create a method for create or edit user for GET type 您可以创建一种方法来为GET类型创建或编辑用户

[HttpGet]
[Route("users/useraddoredit/{id?}")]
public async Task<IActionResult> UserEdit(string id)
{
  if(string.isNullorEmpty(id))
  {
    return View();
  }
  return View(vm);
}

You can create a method for POST type. 您可以为POST类型创建方法。 Here create a new model UserViewModel which has all the attributes. 这里创建一个具有所有属性的新模型UserViewModel。

[Route("users/edit/{id}/{isUserEdit?}")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UserEdit(UserViewModel vm, bool isUserEdit)
{
    if(isUserEdit)
    {
       return View(vm);
    }    
    else
    {
        //logic for add user
    }
}

PS:But its not recommended to use this approach. PS:但不推荐使用这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM