Reputation: 1355
In ASP.NET MVC, I have 2 "Login" actions that do almost the same thing but they use a different model and return different views. How can I make sure I follow the DRY rule with these actions?
I tried making a separate function that receives the parameters I need but I can't do that with the model being different.
Here are the two actions:
Public Function Login(model As AccountViewModels.InternalLoginViewModel, location As String) As ActionResult
...
If ... Then
...
Return View(model)
End If
If Not ModelState.IsValid Then
Return View(model)
End If
...
If authenticationResult.Success Then
...
ElseIf ... Then
Dim appUser As ApplicationUser = authService.GetApplicationUser(model.EmailUsername)
...
ElseIf .. Then
Return RedirectToAction("OldAppnuserRegister", MVC_CONTROLLER_ACCOUNT, authenticationResult.OldUser.GetRouteValues(location))
ElseIf ... Then
Return RedirectToAction("Index", MVC_CONTROLLER_MANAGE, New With {.userKey = authService.GetApplicationUser(model.EmailUsername).Key.ToString})
End If
...
Return View(model)
End Function
---------------
Public Function RepLogin(model As RepSessionViewModels.InternalLoginViewModel, location As String) As ActionResult
...
If ... Then
...
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End If
If Not ModelState.IsValid Then
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End If
...
If authenticationResult.Success Then
...
ElseIf ... Then
Dim appUser As ApplicationUser = authService.GetApplicationUser(model.Email)
...
ElseIf ... Then
Return RedirectToAction("OldRepRegister", MVC_CONTROLLER_ACCOUNT, authenticationResult.OldUser.GetRouteValues(location))
ElseIf ... Then
Return RedirectToAction("Index", MVC_CONTROLLER_MANAGE, New With {.userKey = authService.GetApplicationUser(model.Email).Key.ToString})
End If
...
Return View("~/Views/RepSession/SelectProvider.vbhtml", model)
End Function
I replaced repeated code by "..." to make it more readable. All help is appreciated. Thank you.
Upvotes: 1
Views: 96
Reputation: 1355
The answers I got pointed towards a design issue and I'm going to assume it is and there is no other way to solve my problem.
What I ended up doing is make the Model/Action/View the same and removed RepLogin entirely. Changed the user requirement from accepting only emails in RepLogin to also accepting username. The magic strings are determined depending on a Boolean saying if we are in RepLogin or normal Login.
Not the perfect solution because I had to change things I did not originally want to change, but it works.
Upvotes: 0
Reputation: 61
It's not clear what you need based on posted code.
But assuming you really need two different models, You have different ways to do it, please refer to this post http://www.dotnet-stuff.com/tutorials/aspnet-mvc/way-to-use-multiple-models-in-a-view-in-asp-net-mvc. If you still have questions just please properly clarify the differences/similarities between your views/models.
In my personal experience, this scenario is just a bad design, you can always have a base model, base view and partial view to do it properly in order to avoid duplicated code.
Upvotes: 1