0

I'm trying to submit a form to my account controller. I am getting the following errors: I've looked everywhere and cannot seem to find the solution.

1

$ and JQuery are not defined.

2

server responding with a 404 error (which I'm sure is related to #1)

NOTE: I understand my controller is not setup to handle the request at the moment. What I need assistance with is trying to get the request through to the server.

Thanks Stack in advance for your help!

View:

    <reference path="jquery.min.js" />
<reference path="kendo.all.min.js" />
          @{
            ViewBag.Title = "Register";
            }
      
    @section styles {
    <link href="@Url.Content("~/Content/Views/Form.css")" rel="stylesheet" type="text/css" />
}


<script type="text/javascript">
    $('#"ButtonSubmitForm"').click(function () {
        submit();
    }
    );



</script>


<div class="middle-center">
    <div class="form-center">
        @{
            Html.Kendo().Window()
        .Name("registerForm")
        .Title("UserProfileWndw")
        .Draggable()
        .Width(500)
        .Actions(actions => { })
        .Iframe(true)
        .Content(
        @<text>
            @using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary()

    <fieldset>
        <legend>Registration Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password)
            </li>
            <li>
                @Html.LabelFor(m => m.ConfirmPassword)
                @Html.PasswordFor(m => m.ConfirmPassword)
            </li>
            <li>
                @Html.LabelFor(m => m.EmailAddress)
                @Html.TextBoxFor(m => m.EmailAddress)


            </li>
            <li>
                @Html.LabelFor(m => m.OrganizationId)
                @Html.DropDownListFor(m => m.OrganizationId, new SelectList(ViewBag.Organizations, "OrganizationId", "Name"), string.Empty)
            </li>

        </ol>
        <input type="submit" id="ButtonSubmitForm"  value="Add user" class="k-button" />
        <!-- clear button-->
        <!-- cancel button-->
    </fieldset>
            }
        </text>
).Render();
        }
    </div>
</div>
     
    
    function submit() {
    
    
        $(document).ready(function(){
        
            var form = $("#registerForm");
           
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: "Account/AdminReg/",
                data: form.serialize(),
                success: function (data) {
                    alert(data);
                }
            });




        }
            )
        }
    

    </script>

Controller:

 public ActionResult AdminReg(Register model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
               

                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Organization_OrganizationId = model.OrganizationId, IsApproved = false, EmailAddress = model.EmailAddress }, true);

                // add user to org user group if specified
                Organization org = db.Organizations.Include("Groups").Include("Groups.Members").SingleOrDefault(o => o.OrganizationId == model.OrganizationId);
                if (org != null)
                {
                    UserProfile newUser = db.UserProfiles.Single(u => u.UserName == model.UserName);
                    org.Groups.Single(g => g.Name == "Users").Members.Add(newUser);
                    db.SaveChanges();

                }

                if (User.IsInRole("Admin") || User.IsInRole("Administrators"))
                {
                    return RedirectToAction("ReadUsers", "UserManagement");
                }



                //WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("RegistrationSuccessful", "Account");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        return PartialView("UserAddAdminTemplate");
    }

3 Answers 3

1

You have not included the jquery file (<reference path="jquery.min.js" /> is not enough). Add them using @Scripts.Render("~/bundles/jquery") as indicated in Osadellah'` answer.

Then move $('#ButtonSubmitForm').click(function () { ...}); to the bottom of the page (or wrap in $(document).ready()), because the button does not exist at the time you currently render the script.

Finally, always construct your url's using the UrlHelper (your missing the leading / from the url).

  @Scripts.Render("~/bundles/jquery") // or <script src="~/Scripts/jquery-1.9.1.js"></script> 
  <script type="text/javascript"> // immediately before the closing body tag
    var form = $("#registerForm");
    var url = '@Url.Action("AdminReg", "Account");
    $('#ButtonSubmitForm').click(function() {
      $.post(url, form.serialize(), function(data) {
        alert(data);
      });
    });
  </script>
</body>
0
0

It looks like you didn't include jquery correctly. Instead reference try include in your View :

<script src="~/Scripts/jquery.min.js"></script>

where "~/Scripts/jquery.min.js" is the path to your jquery library.

1
  • I've added a reference to the entire script library and still to no avail.
    – cyclical
    Feb 20, 2015 at 15:52
0

look at your bundleconfig and check if Jquery is referenced there. Here's an example:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*",
                    ));

also check your default shared layout page and make sure they are rendered (Try to post them at the end of your page) here is an example:

    @Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.