2013-05-17 131 views
5

我试图使用Ajax.BeginForm但没有任何成功。我无法让我的表单正常工作。我的控制器操作“更新测试”从未被称为我不知道为什么。我遵循很多教程,但仍然遇到同样的问题。感谢您的帮助 !Ajax.BeginForm与ASP.NET MVC 4不调用控制器操作

我的模型:

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> 

回答

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" /> 
} 
+0

谢谢你的帮忙! – user2037696

相关问题