MVC Ajax renders full page postback

Last post 04-10-2009 9:30 AM by imperialx. 0 replies.

Sort Posts:

  • MVC Ajax renders full page postback

    04-10-2009, 9:30 AM
    • Member
      285 point Member
    • imperialx
    • Member since 06-28-2007, 10:07 AM
    • Posts 802

     Hi,

        I have this JQuery Form plugin combine with MVC Ajax but upon submission, it renders full page postback.

    ---

    index.aspx

    <%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
        Home Page
    </asp:Content>

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <%=ViewData("Message")%>
    <br /><br />
    <%  Html.RenderPartial("submitForm")%>
    <br /><br />
    </asp:Content>

    submitForm.ascx
    <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl" %>

    <%  Ajax.BeginForm("", New AjaxOptions With {.UpdateTargetId = "result"})%>

    <div id="ajaxForm" style="margin-top:150px">
    <span id="result"></span>
        <br />
        <%=Html.TextArea("bComment", "", New With {.class = "rte", .rows = "10", .cols = "50"})%>
        <br />
        <%=Html.TextBox("submit", "Submit Comment", New With {.type = "submit"})%>

    </div>

    <%  Html.EndForm()%>
       
        <script src="<%= Url.Content("~/scripts/jquery-1.3.2.min.js") %>" type="text/javascript"></script>
        <script src="<%= Url.Content("~/scripts/jquery.rte.js") %>" type="text/javascript"></script>     
           
        <script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>  
        <script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>  
       
        <style type="text/css">
            .rte{background-color: white;border: 1px solid #999}
        </style> 
        <script type="text/javascript">
            $(document).ready(function() {
                var arr = $('#bComment').rte({ width: 450, height: 200 });
            });               
        </script>

    HomeController.vb
    <HandleError()> _
    Public Class HomeController
        Inherits System.Web.Mvc.Controller
        Private _service As IService.ICommentService

        Public Sub New()
            _service = New IService(Me.ModelState)
        End Sub

        Function Index() As ActionResult
            ViewData("Message") = "Welcome to ASP.NET MVC!"

            Return View()
        End Function

        <ValidateInput(False), AcceptVerbs(HttpVerbs.Post)> _
        Public Function Index(ByVal formValues As FormCollection)

            If Not _service.ValidateComments(formValues) Then
                ViewData("Message") = "Error Submitted"
            Else
                ViewData("Message") = "Success"
            End If

            Return View()

        End Function
    End Class

    IService.vb
    Public Class IService
        Implements ICommentService
        Private _modelState As ModelStateDictionary

        Public Sub New(ByVal modelState As ModelStateDictionary)
            _modelState = modelState
        End Sub

        Protected Function ValidateComments(ByVal formValues As FormCollection) As Boolean Implements ICommentService.ValidateComments
            Dim c As String = formValues.Item("bComment")

            If formValues.Item("bComment").Trim().Length = 0 Then
                _modelState.AddModelError("comment", "Comment is required.")
            End If

            If (Not _modelState.IsValid) Then
                Return False
            End If

            Return _modelState.IsValid

        End Function

        Public Interface ICommentService
            Function ValidateComments(ByVal formValues As FormCollection) As Boolean
        End Interface
    End Class

    jquery.rte.js (111 lines - uncompressed)
    jQuery.fn.rte = function(a, b) {
        if (!b || b.constructor != Array) {
            b = new Array()
        }
        $(this).each(function(c) {
            var d = (this.id) ? this.id : b.length;
            b[d] = new lwRTE(this, a || {})
        });
        return b
    };
    var lwRTE_resizer = function(a) {
        this.drag = false;
        this.rte_zone = $(a).parents(".rte-zone")
    };
    var lwRTE = function(a, b) {
        this.css = [];
        this.css_class = b.frame_class || "";
        this.base_url = b.base_url || "";
        this.width = b.width || $(a).width() || "100%";
        this.height = b.height || $(a).height() || 350;
        this.iframe = null;
        this.iframe_doc = null;
        this.textarea = null;
        this.event = null;
        this.range = null;
        this.toolbars = {
            rte: "",
            html: ""
        };
        this.controls = {
        };
        $.extend(this.controls.rte, b.controls_rte || {});
        $.extend(this.controls.html, b.controls_html || {});
        $.extend(this.css, b.css || {});
        if (document.designMode || document.contentEditable) {
            $(a).wrap($("<div></div>").addClass("rte-zone").width(this.width));
            this.textarea = a;
            this.enable_design_mode()
        }
    };
    lwRTE.prototype.activate_toolbar = function(c, a) {
        var b = this.get_toolbar();
        if (b) {
            b.remove()
        }
        $(c).before($(a).clone(true))
    };
    lwRTE.prototype.enable_design_mode = function() {
        var a = this;
        a.iframe = document.createElement("iframe");
        a.iframe.frameBorder = 0;
        a.iframe.frameMargin = 0;
        a.iframe.framePadding = 0;
        a.iframe.width = "100%";
        a.iframe.height = a.height || "100%";
        a.iframe.src = "javascript:void(0);";
        a.iframe.id = "";
        a.iframe.name = "";
        if ($(a.textarea).attr("class")) {
            a.iframe.className = $(a.textarea).attr("class")
        }
        if ($(a.textarea).attr("id")) { }
        if ($(a.textarea).attr("name")) {
            a.iframe.title = $(a.textarea).attr("name")
        }
        var f = $(a.textarea).val();
        $(a.textarea).hide().after(a.iframe).remove();
        a.textarea = null;
        var c = "";
        for (var b in a.css) {
            c += "<link type='text/css' rel='stylesheet' href='" + a.css[b] + "' />"
        }
        var g = (a.base_url) ? "<base href='" + a.base_url + "' />" : "";
        var d = (a.css_class) ? "class='" + a.css_class + "'" : "";
        var j = "<html><head>" + g + c + "</head><body " + d + " style='padding:5px'>" + f + "</body></html>";
        a.iframe_doc = a.iframe.contentWindow.document;
        try {
            a.iframe_doc.designMode = "on"
        } catch (h) {
            $(a.iframe_doc).focus(function() {
                a.iframe_doc.designMode()
            })
        }
        a.iframe_doc.open();
        a.iframe_doc.write(j);
        a.iframe_doc.close();
        $(a.iframe).parents("form").submit(function() {
            a.disable_design_mode(true)
        })
    };
    lwRTE.prototype.disable_design_mode = function(b) {
        var a = this;
        a.textarea = (b) ? $('<input type="hidden" />').get(0) : $("<textarea></textarea>").width("100%").height(a.height).get(0);
        if (a.valueOf = !null) {
            if (a.iframe.className) {
                a.textarea.className = a.iframe.className
            }
            if (a.iframe.id) {
                a.textarea.id = a.iframe.id
            }
            if (a.iframe.title) {
                a.textarea.name = a.iframe.title
            }
            $(a.textarea).val($("body", a.iframe_doc).html());
            $(a.iframe).before(a.textarea)
        }
    };
    lwRTE.prototype.create_toolbar = function(d) {
        var c = this;
        var b = $("")
        var h, a
    };
    ---

     

    best regards,

    imperialx

     

Page 1 of 1 (1 items)