开发者

JavaScript action result no longer working after converting to partial view

Early yesterday, the following validation notice was working correctly. Then we converted the Index view where the request for this action originates to use a partial view, and th开发者_如何学JAVAe Delete ActionLink is now inside that partial view, and now the string argument to the JavaScript method call is rendered literally and as the only content on the 'destination' Delete view.

    public ActionResult Delete(int id)
    {
        var perm = JobCardService.CheckBusinessRules(id);
        if (!string.IsNullOrEmpty(perm))
        {
            return JavaScript("NotifyFailure('You may not delete this Installation: " + perm + "', false, 2000);");
        }
        JobCardViewData viewData = ViewDataFactory.CreateBaseViewData<JobCardViewData>("Installation List");
        return View("Delete", viewData);
    }

The Filter action returns the partial view, and is requested as below:

<div class="editor-field">
    <% using (Ajax.BeginForm("Filter", "JobCard", new AjaxOptions { UpdateTargetId = "jobList" }))
       { %>
    <%= Html.DropDownListFor(model => model.RequesterId, new SelectList(Model.RequesterList, "RequesterID", "CompanyName", Model.RequesterId), new { onchange = "$('#Select_Save').click();" })%>
    <input id="Select_Save" type="submit" value="Save" style="display: none" />
    <% 
        }%>
</div>


If the action method is responsible for returning a view, seems like the response shouldn't be returning a JavaScript if in error because no underlying ASP.NET page would be served, which means that you would see it as literal text.

Consider assigning the method call to ViewData, and in your client do something like:

<% if (ViewData["X"] != null) { %>
    <script type="text/javascript">
        <%= ViewData["X"] %>
    </script>
<% } %>

Calling VIewData["X"] like I do should render the JavaScript code directly and get directly executed when parsed.

I think that might work; you can always utilize other mechanisms like eval to parse content, or do whatever else you might need....


Refer to the comment of this question ASP.NET MVC Javascript ActionResult

The other aspect is that using this return type is considered to be an anti-pattern and should be avoided. The suggested approach is to use a Json result.

  1. Working example for JavaScriptResult in asp.net mvc
  2. http://devlicio.us/blogs/billy_mccafferty/archive/2009/02/07/beware-of-asp-net-mvc-javascriptresult.aspx

Edit:

Since javascript is being returned from the Controller, an alternative would be to send script back to the browser that redirects the user to the correct page.

   public ActionResult Delete(int id)
    {
        var perm = JobCardService.CheckBusinessRules(id);
        if (!string.IsNullOrEmpty(perm))
        {
            return JavaScript("NotifyFailure('You may not delete this Installation: " + perm + "', false, 2000);");
        }
        // you may need to do a bit more to create a URL in the form of http://...
        UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);
        string url = u.Action("ActionName","ControllerName", new{id=1}); // the new Action will return the delete view
        return Javascript(String.Format("window.location =""{0}"",url);
    }

Refer to Creating a URL in the controller .NET MVC for more on the UrlHelper.


This may not be the best way to do this, but every other answer I have come across has required extensive rework to achieve. This requires one small, simple change and works exactly as required. All I had to do was change the Delete action link from this:

<%= Html.ActionLink("Delete", "Delete", new { id = item.InstallationDBNumber }) %>

to this:

<%= Ajax.ActionLink("Delete", "Delete", new { id = item.InstallationDBNumber }, new AjaxOptions { HttpMethod = "Get" }) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜