0

I have a dropdownlist with different soccer Clubs from my database. When I select one, an ajax call will be made to show all the Games that Club will play in.

The problem is whenever I'm logged on as an AspNetUser I automatically get logged off the second I submit the dropdownlist.

When I'm not logged in everything works as it should.

I'm really confused why exactly this happens.

Controller

[Authorize]
public class WedstrijdController : Controller
{

    [AllowAnonymous]
    public ActionResult Index()
    {
        ploegService = new PloegService();
        ViewBag.PloegId = new SelectList(ploegService.All(),
            "Id", "Naam");
        return View();
    }

    [AllowAnonymous]
    public ActionResult IndexAjax(int? ploegId)
    {
        if (ploegId == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        wedstrijdService = new WedstrijdService();
        var wedstrijden = wedstrijdService.WedstrijdenByPloeg(Convert.ToInt16(ploegId));
        return PartialView("_SearchWedstrijdenPartial", wedstrijden);
     }
}

Index.cshtml

@{
    ViewBag.Title = "Lijst";
}


<h2>Wedstrijden</h2>

<div class="panel panel-default">
    <div class="panel-heading">Kies een wedstrijd</div>
    <div class="panel-body">
        @using (Ajax.BeginForm("IndexAjax", "Wedstrijd",
        new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            LoadingElementId = "ajax-loader",
            UpdateTargetId = "searchresults",
        }))
        {
        <div class="form-horizontal">
            <div class="form-group">
                @Html.Label("Ploeg", htmlAttributes: new { @class = "control-label col-md-1" })
                <div class="col-md-10">
                    @Html.DropDownList("PloegId", null, "---Kies ploeg---", htmlAttributes: new { @class = "form-control" })
                    <img id="ajax-loader"
                         src="@Url.Content("~/content/Images/ajax-loader.gif")"
                         style="display: none" />
                </div>
            </div>
        </div>
        }
        <div id="searchresults"></div>
    </div>
</div>


@section scripts
{@Scripts.Render("~/bundles/jqueryUnobtrusive")
    <script>
        $("#PloegId").on("change",
            function() {
                $("form").trigger("submit");
            });
    </script>
}

_SearchWedstrijdenPartial.cshtml

@model IEnumerable<VoetbalMVC.Models.Wedstrijd>

<table class="table">
    <tr>
        <th>
            Thuisploeg
        </th>
        <th>
            Bezoekersploeg
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stadion)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Datum)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Ploeg1.Naam)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Ploeg.Naam)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Stadion.Naam)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Datum)
        </td>
        <td>
            @Html.ActionLink("Order tickets", "Detail", new { wedstrijdId = item.Id })
        </td>
    </tr>
}
</table>

2 Answers 2

2

Indeed, by using the $("form") jQuery selector you are targeting all form elements on the page. You can verify the forms being submitted from the client to the server with tools like Fiddler, Postman, or the developer tools of your browser (by hitting F12 or CTRL+Shift+I on Windows/Linux, or Command+Option+I on Mac).

As far as the logout form is concerned, if you haven't customized the folder/file structure of your MVC project, it should be located in Views/Shared/_LoginPartial.cshtml:

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
        // ...
    }
}

..so it is generating a form with an ID of #logoutForm, but since you are submitting all forms at once, you are subsequently logging out the user each time.

This means you need to modify the call to only submit the form you want. Assuming the ID of your form being AjaxForm, you would do:

$("#AjaxForm").trigger("submit");
Sign up to request clarification or add additional context in comments.

Comments

1

This piece of code was causing the error

 $("form").trigger("submit");

As you can see this includes all forms. I suspect this means the logout button is included in some sort of form which triggers this action when logged in. Simply add an Id to your AjaxForm and only trigger the submit of that AjaxForm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.