带有ASP.NET MVC 4的Ajax.BeginForm没有调用控制器动作

时间:2013-05-17 19:19:08

标签: c# asp.net .net asp.net-mvc ajax.beginform

我正在尝试使用Ajax.BeginForm但没有任何成功。我不能让我的表格正常工作。我的控制器动作“UpdateTest”永远不会被调用我不知道为什么。我遵循了许多教程但仍然遇到了同样的问题。谢谢你的帮助!

我的模特:

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; }  
}

我在ColorController中的行动

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

我的观点

@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>

1 个答案:

答案 0 :(得分:13)

以这种方式试试....

@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" />
}