Ajax.BeginForm with ASP.NET MVC 4 not calling controller action

user2037696 picture user2037696 · May 17, 2013 · Viewed 14.6k times · Source

I'm trying to use Ajax.BeginForm but without any success. I cannot get my form to work properly. My Controller Action "UpdateTest" is never called I don't know why. I followed many tutorial but still get the same problem. Thank you for your help !

My Model:

public class TestModel
{
    public ObjectId _id { get; set; }
    public int orange { get; set; }
    public int blue { get; set; }
    public int red { get; set; }
    public int yellow { get; set; }
    public int white { get; set; }
    public float green { get; set; }
    public float pink { get; set; }  
}

My Action in ColorController

 [HttpPost]
    public void UpdateTest(TestModel tmp)
    {
       ...
       ...
    }

My View

@model Project.Models.TestModel


@using (Ajax.BeginForm(new AjaxOptions()
{
    HttpMethod = "POST",
    Url = Url.Action("UpdateTest", "Color")
}))
{
        @Html.TextBoxFor(model => model._id)
        @Html.TextBoxFor(model => model.orange)
        @Html.TextBoxFor(model => model.blue)
        @Html.TextBoxFor(model => model.red)
        @Html.TextBoxFor(model => model.yellow)
        @Html.TextBoxFor(model => model.white)
        @Html.TextBoxFor(model => model.green)     
        @Html.TextBoxFor(model => model.pink)

        <input type="submit" value="Submit" />
}

Javascript

<script type="text/javascript" src="/Scripts/jquery.unobtrusive-ajax.min.js">
</script>

Answer

Nick Albrecht picture Nick Albrecht · May 17, 2013

Try it this way....

@using (Ajax.BeginForm("UpdateTest", "Color", new AjaxOptions() { HttpMethod = "POST" }))
{
    @Html.TextBoxFor(model => model._id)
    @Html.TextBoxFor(model => model.orange)
    @Html.TextBoxFor(model => model.blue)
    @Html.TextBoxFor(model => model.red)
    @Html.TextBoxFor(model => model.yellow)
    @Html.TextBoxFor(model => model.white)
    @Html.TextBoxFor(model => model.green)     
    @Html.TextBoxFor(model => model.pink)

    <input type="submit" value="Submit" />
}