November Happy Hour will be moved to Thursday December 5th.

MVC Html.BeginForm and multiple languages

Vote:
 

So how does Html.BeginForm build the action url for the form element.

It uses the UrlHelper class. My guess is that it will somehow try to identify the url for a specific controller/action + routevalues using the routetable. But i am pretty sure that it does not take langauge under consideration when doing this (hint: it does not). 

Well we did have problems with this, meaning submitting a simple form in a multi language episerver 7 mvc site.

We can solve it by manually creating the form element and getting the url for the page using http://joelabrahamsson.com/episerver-7-and-mvc-getting-the-url-for-a-page/ and adding the actionname to it.

But do anyone have alternative solution for this challenge?

I did try to make my own .BeginForm() extension for this, but of course some of the methods involved is marked as private and internal in the System.Web.Mvc assembly, so i gave up (not worth the effort imho)

#73967
Aug 15, 2013 16:10
Vote:
 

I think you can use html.translate for multiple languages. And just put xpath in language xml files.

#73974
Aug 16, 2013 3:16
Vote:
 

Ok i think you missed the point. It has nothing to do with localization/translation.

Html.BeginForm writes out the form elements in html to be able to post something back to an action inside some controller. Typically you will post to an action in the same controller (not always i know)

On the form in the html you usally specifiy which url to post the result to, and usally Html.BeginForm will take care of this when you specify actionname and controllername. But the problem here is that we have multiple languages (read: urls) pointing to the same action/controller in the RouteTable.

So my form is being submitted to the wrong language of the page, because Html.BeginForm picks up the wrong language url.

#73986
Aug 16, 2013 9:21
Vote:
 

Have you setted different languages in your form action url?

Do you just use just like the following?

@using (Html.BeginForm()) {

     <div >
         <fieldset>
             <div class="editor-label">
                 @Html.LabelFor(m => m.Account)
             </div>
             <div class="editor-field">
                 @Html.TextBoxFor(m => m.Account)
                 @Html.ValidationMessageFor(m => m.Account)
             </div>
            <div class="editor-label">
                 @Html.LabelFor(m => m.Password)
             </div>
             <div class="editor-field">
                 @Html.PasswordFor(m => m.Password)
                 @Html.ValidationMessageFor(m => m.Password)
             </div>  
            <p>
                 <input type="submit" value="submit"  />
             </p>
         </fieldset>
     </div>
 }
#73987
Edited, Aug 16, 2013 9:29
Vote:
 

I think you can use it like this:

@using (Html.BeginForm(new { Action = Model.ActionUrl }))
{
    // Your form...
}

#73988
Aug 16, 2013 9:31
Vote:
 

Yeah we could do it like that, but it is just as bad as creating the form element manually and specifying the url.

What would be nice is an EPiServer html helper extension that could take the current language into consideration, but i am guessing i am asking for too much :)

One day when I have oceans of time i will make it (read: never)

#73991
Aug 16, 2013 10:13
Vote:
 

My colleague just found the solution when he was looking at linking to other language branches using Html.PageLink.

Adding langauge routevalue did the trick:

@using (Html.BeginForm("SubMitting", "Test", new {language= ContentLanguage.PreferredCulture.Name}))
{
}

    

#73993
Aug 16, 2013 10:54
Vote:
 

Cool man, thanks for sharing

#73994
Aug 16, 2013 11:17
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.