0

Need some ideas for the following problem: My View called MyView (code adjusted to post):

    if(condition== A)
 @using (Html.BeginForm())
                {
                    @Html.Sitecore().FormHandler("Registration", "ActionA")
                    form html
                }

else if(condition == B)

@using (Html.BeginForm())
                {
                    @Html.Sitecore().FormHandler("Registration", "ActionB")
                    form html
                }

so each form post to a different action on my controller.

Controller simplified for the post:

[HttpPost]
        public ActionResult ActionA(MyModel model)
        {
                 //an error happened here and I want to return to the original view with the error message
                 if(error){
                     ModelState.AddModelError("Error", "")
                     return PartialView("MyView", model);
                 }
        }

[HttpPost]
        public ActionResult ActionB(MyModel model)
        {
                 //an error happened here and I want to return to the original view with the error message
                 if(error){
                     return PartialView("MyView", model);
                     ModelState.AddModelError("Error", "")
                  } 
        }


        public ActionResult MyView(MyModel model)
        {
                 //Instantiate model here
                  .... 
                 return PartialView("MyView", model);
        }

Since I'm posting to a different action "ActionA" for example, and an error happened I'd like to show an error message to the user on the same original view.

The problem is that it won't find my original View I'm thinking because I'm executing it from another action. The error message is the standard

"The partial view 'MyView' was not found or no view engine supports the searched locations"

This is a simplified explanation but for example using a single post action to handle both is not an option here. Any thoughts?

  • 1
    Why don't you split the forms into 2 separate controls and use the Rules Engine to hide each one as necessary? – jammykam Jan 11 '18 at 23:23
  • Considering the project/deadline I'm looking for the solution that would bring the least amount of changes necessary. But it's an interesting idea. Can you elaborate @jammykam – Diego Jan 11 '18 at 23:30
  • 1
    The architect inside me just died a little. So did a butterfly in Japan, I suspect. – Mark Cassidy Jan 14 '18 at 11:11
1

Indeed, as @jammykam said, you should split the conditions in 2 different renderings and handling the visibility through rules engine. If you think that conditions are quite complex to be adapted for rules engine then split the functionality in 2 views.

For other side, normally for dealing with ViewLocations on POST actions we create a custom dynamic route called "MyProjectApi/{controller}/{action}" indicating the ViewLocations following this convention "Components/{0}/{1}" which 0 is the controller and 1 is the action. On this way you can separate the standard route resolving in Sitecore for POST actions, which are nothing to do with Sitecore Content Tree items.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.