How to add a form in the _Root.cshtml ?.

Vote:
 

Hello,

I need to add a small form with a textfield for email and login button, in the site header in the _Root.cshtml. 

I tried to post from it to an action defined in the StartPageController, but it doesn't work, I get 404. What the best way to implement this?

using (Html.BeginForm("Login", "StartPageController", FormMethod.Post))
{
    <input required="required" type="text" id="loginHint" name="loginHint" value="" placeholder="Enter email">
    <button id="loginBtn" class="btn" type="submit">Login</button>
}

thanks

jackyjoy

#261357
Aug 31, 2021 16:53
Vote:
 

You should be able to post to an action in a page as you're trying to do. There can be issues with posting to child controllers but pages usually work.

Can you post your controller code?

#261358
Aug 31, 2021 17:04
Vote:
 

From what I've read around, you need to structure your BeginForm without a controller but with a reference to the target content type like so:

@using (Html.BeginForm("Login", null, new { node = ContentReference.StartPage }, FormMethod.Post))
{
      // Form elements go here
}

Alternatively place the Login action on a base controller that all of your Page type controllers inherit from.

#261435
Sep 01, 2021 8:53
Vote:
 

I think you should be able to do that if you create a plain MVC controller (inherit direct from Controller) and call its action method (epi's controller won't work).

#261897
Edited, Sep 08, 2021 8:44
Vote:
 

While Stotty's response looks to be correct, I'll just throw out another option:

Do you need to use @Html.BeginForm()? How about just writing out the form tag?

<form method="post" action="@Url.ContentUrl(ContentReference.StartPage)Login">
    <input required="required" type="text" id="loginHint" name="loginHint" value="" placeholder="Enter email">
    <button id="loginBtn" class="btn" type="submit">Login</button>
</form>

I know it looks odd to just have the action name added after the closing parenthesis, but I haven't had any issues with this way of writing it.

#263055
Sep 14, 2021 3:50
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.