Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller generate few threads, although not asked

For several days I just can not find the problem itself, which is really driving me crazy.

I have asp.net (mvc4) web application, where there are several index pages (showing list), when clicking on any item in the list, it returns edit view page. In the edit page (in the view itself) - there is a submit button, which should update the db and redirect to the index page.

(At first, that "submit" was loaded with all html edit code via partial view, but I changed it so i can redirect to index page - because "child actions are not allowed to perform redirect actions").

So the problem is that controller does not redirect to the index page.

When I put a breakpoint in the post function in controller, I see that few threads runs the code although not asked for threads, and if i stand with the cursor on one of the processes debug arrows, I can see message "the process or thread has changed since last step".

Somehow, I don't know how, I solved the problem in one page (dont know how, because I dont know what caused this), but sometimes (randomly) it's appears again, and in the other page I did not manage to solve it.

Here is some code from controller and from view:

Controller:

[HttpPost]
public ActionResult Edit([ModelBinder(typeof(OpportunityBinder))] OpportunityModel draft)
{
    try
    {
        if (Request.Params["cancel"] != null)
        {
            return RedirectToAction("index", Utils.CrmDB.GetOpportunities());
        }

        if (draft.IsValid())
        {
            if (Utils.CrmDB.UpdateOpportunity(draft))
                return RedirectToAction("Index", Utils.CrmDB.GetOpportunities());
        }

        return View(draft);
    }
    catch
    {
        return View();
    }
}

View:

@using (Html.BeginForm()) 
{

    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset>
       /* Some divs */
        <p>
            <input type="submit" value="Update" />
        </p>
    </fieldset>


    <fieldset>
   /* Some partial views*/
       /* loaded using @Html.Action() */
    </fieldset>

}

@section Scripts
{

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
}

Partial view code:

@model Genius_crm.Models.ActivityListModel

<p>
    <button id="dlgAddActivity">
        New
    </button>
</p>

<div>
    <table>
       /* some tr and td */
    </table>
</div>



<script type="text/javascript">

    $(document).ready(function () {
        $('#dlgAddActivity').each(function () {
            var $link = $(this);

            var pathname = window.location.pathname;
            var parts = pathname.split('/')
            var sub = parts[3]

        var $dialog = $('<div id="addDialog"></div>')
           .load('@Url.Action("AddActivityToOppPartial", "Opportunity")[email protected]&[email protected]')
            .dialog({
                autoOpen: false,
                title: $link.attr('title'),
                width: 758,
                height: 265,
                resizable: false,
                //close: function (event, ui) { window.location.reload();}
            });

        $link.click(function () {
            $dialog.dialog('open');

            return false;
        });
    });
});

So - I Hope I have made my problem clear. I've been through some posts on the subject, and none of them helped me. The problem appears also in chrome, and in IE too.

EDIT #1 - When commenting out the partial views the problem disappears in all pages!

EDIT #2 - Seems that there is a problem with buttons loaded in partials which using other controller actions.

EDIT #3 - I have added code of partial view loaded with @Html.Action(), which include one script for jquery-ui dialog.

like image 343
ayheber Avatar asked Mar 19 '14 09:03

ayheber


1 Answers

Oh lord ! It was a tough week, But at least I learned a lesson.

The problem was composed of two parts:

(1) EF and DataContext management (I re-tag the question).
I used a single static instance (singleton) of DataContext, and it turned out to be ineffective and problematic.

So I was looking for some information and found that there are better implementations for DataContext management (you can read here) and now - everything seems right!

(2) jQuery validating all partials on form submit. I had to find a way to handle it.

A solution of one part without the other - did not yield a good result.

like image 63
ayheber Avatar answered Oct 16 '22 08:10

ayheber