How to put a form in the _Layout view?

Asked

Viewed 65 times

1

I’m trying to make a website with support for two languages.

First test I did was to put this code inside a view common, the index of my control Home.

Worked perfectly.

When I put the same code in a partial view and call him in the view _layout it doesn’t work. Can anyone tell me what I’m doing wrong?

Below is the code as it is in partial view, what doesn’t work is form Submit.

@{
    var culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name.ToLowerInvariant();
}

@helper selected(string c, string culture)
{
if (c == culture)
{
        @:checked="checked"
        }
}

@using (Html.BeginForm("SetCulture", "Home"))
{
    <fieldset>
        <legend></legend>
        <div class="control-group">
            <div class="controls">
                <label for="en-us">
                    <input name="culture" id="en-us" value="en-us" type="radio" @selected("en-us", culture) /> English <img src="http://st.xptotube.com/img/180x135/l/blank.png" class="flag flag-us" alt="United States of America" />
                </label>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <label for="pt-br">
                    <input name="culture" id="pt-br" value="pt-br" type="radio" @selected("pt-br", culture) /> Portugues <img src="http://st.xptotube.com/img/180x135/l/blank.png" class="flag flag-br" alt="Brasil" />
                </label>
            </div>
        </div>

    </fieldset>
}



@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        (function ($) {
            $("input[type = 'radio']").click(function () {
                $(this).parents("form").submit(); // post form
            });

        })(jQuery);
    </script>
}

I’m calling him that on _Layout:

@Html.Partial("_Language")
  • To Action SetCulture of HomeController comes to be called?

  • It doesn’t get called no.

1 answer

2


Partials can’t stand it @section Scripts. The correct thing would be for this code to stay in _Layout or any View, but it would have to be modified. Don’t leave it on Partial.

My suggestion to _Layout:

    <script type="text/javascript">
        (function ($) {
            $("input[type='radio'][name='culture']").click(function () {
                $(this).parents("form").submit(); // post form
            });

        })(jQuery);
    </script>
  • I put this your code as the last thing before closing the body, I took mine from the partil, but it didn’t work, the impression is that it does not execute the script...

  • Yeah. Could be that parents there.

  • I’m not a javascript connoisseur, it works within a normal view. I tried with document.getbyid("form") putting the an id in the form, but did not get tb, I find it strange not to be able to put a form in the Layout, as I would to have a function like this?

  • 1

    I did not see q vc took the @Section Scripts code without it worked. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.