Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 233
user2037696 Avatar asked May 17 '13 19:05

user2037696


1 Answers

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" />
}
like image 159
Nick Albrecht Avatar answered Sep 28 '22 04:09

Nick Albrecht