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

1

5

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>
Coincident answered 17/5, 2013 at 19:19 Comment(0)
L
13

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" />
}
Lysine answered 17/5, 2013 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.