Question

I have this display box and link on my view, it displays the hours variable and an edit action link and save action link for each item in the model, each are in their own separate form:

using (Html.BeginForm("SaveChanges", "WeeklyTarget"))
{
    <td id="hoursEdit">
        @Html.DisplayFor(modelItem => item.Hours)
    </td>
    <td>
        @Ajax.ActionLink("Edit", "Index", "WeeklyTarget",
            new {id = item.WeeklyTargetId},
            new AjaxOptions {HttpMethod = "GET", UpdateTargetId = "hoursEdit", InsertionMode = InsertionMode.Replace}) 
        @Html.ActionLink("Save", "SaveChanges", new {weeklytarget = item})
    </td>
}

This link takes the id of the row that it is on and it SHOULD (I'm hoping) follow the AjaxRequest logic in the ActionResult below:

public ActionResult Index(int? id)
{
    //I'd expect this to evaluate as true
    if (Request.IsAjaxRequest())
    {
        var weeklytarget = db.WeeklyTargets.Find(id);
        return PartialView("HoursUpdate", weeklytarget);
    }
    var weeklytargets = db.WeeklyTargets.Include(w => w.User).Include(w => w.Weekly);
    return View(weeklytargets.ToList());
}

However when I select this Edit link, the Request.IsAjaxRequest() evaluates as false and it jumps past, why is this? I've tried throwing in an ! to see what would happen if I forced the code to execute but since it's not recognized as an ajax call the InsertionMode.Replace won't execute properly and I am left stuck on the partial view page that I was trying to return back to the main page. Why is this not being recognized?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top