0

I have asp.net mvc 5 application but I can't get this fairly simple script to work. I am new to this MVC (I am WebForms guy). Basically jQuery is not attaching .click() event to the button to post data to the underlying controller.

Can somebody pinpoint what I am doing wrong?

The View (HelloWorld/Index.cshtml):

@model HelloWorld.Models.HelloWorldModel

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <script src="~/Scripts/jquery-2.1.3.min.js"></script>
</head>
<body>
    <div>
    @using (Html.BeginForm("Welcome","HelloWorld",FormMethod.Post, new {id = "myhelloform"})){

    <span>Name:</span> 
        @Html.TextBoxFor(model=> model.Name)
    <button id="btnPost" type="button">Post</button>
    }
    </div>

    <script>
    var testform = function () {
        var init = function () {
            $("#btnPost").on("click", function () {
                alert("anybody there??");
                submitForm();
            });
        };

        var submitForm = function () {
            $.hit({
                url: "@Url.Action("Welcome", "HelloWorld")",
                data: $("#myhelloform").serializeArray(),
                success: function (response) {
                    alert(response.Message);
                }
            });
        };

        return {
            Init: init
        };
    };

    </script>
</body>
</html>
</html>

The Controller (Controllers/HelloWorldController.cs):

using System.Web.Mvc;
namespace HelloWorld.Controllers
{
    public class HelloWorldController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Welcome()
        {
            return Json("test");
        }
    }
}

The Model (Models/HelloWorldModel.cs)

namespace HelloWorld.Models
{
    public class HelloWorldModel
    {
        public string Name { get; set; }
    }
}
7
  • It looks like your init function is never being called... Add an alert to the top of that function to check. Commented Jan 31, 2015 at 16:07
  • Tried that... no alert is being hit :( Commented Jan 31, 2015 at 16:18
  • dotnetfiddle.net/HCdFXM Works without testform and init Commented Jan 31, 2015 at 16:18
  • @JAT - that seems to work, thanks - i.e. getting rid of testform and init. Please post as answer. Next problem occurs now submitForm() is invoke but nothing happens, i.e. alert(response.Message) never fires? Commented Jan 31, 2015 at 16:28
  • Done, please ask the next problem in a separate question Commented Jan 31, 2015 at 16:31

4 Answers 4

2

Delete the variables testform and init and it works (Also need to use $( document ).ready as @Tbseven mentions):

    $( document ).ready(function() {
                $("#btnPost").on("click", function () 
                                 {
                                    alert("anybody there??");

                                 });
});

Fiddle: https://dotnetfiddle.net/HCdFXM

Sign up to request clarification or add additional context in comments.

Comments

1

You have to place your jquery code inside next function:

$( document ).ready(function() {
  // Here
});

1 Comment

Even that does not work. Same problem in both IE 11 and FF 35.
1
$( document ).ready(function() {
   $("#btnPost").on("click", function () {
       alert("anybody there??");
       submitForm();
   });

   function submitForm () {
         $.hit({
             url: "@Url.Action("Welcome", "HelloWorld")",
             data: $("#myhelloform").serializeArray(),
             success: function (response) {
                 alert(response.Message);
             });
    };
});

2 Comments

Thanks @JeffWatkins - JAT beat you to it though, so I awarded the answer to him.
No problem, I'm just here to help (and to keep my eye in, as I rarely get chance to code nowadays).
1

You can use input instead of button and there is no need to add click event for that.

<input type="submit" id="btnPost" />

4 Comments

I could but basically I simplified my question here on SO - the reason for using <button> in this case is because of some css rules related to only <button>.
I agree, you should catch submit event +1
@Sha: you can add that too for that via applying this simple rule: input[type="submit"] { //your rule here.. }
Yes but in this project it would require a lot of work to switch from button to input... but thanks anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.