Ajax.BeginForm没有发布模型

时间:2015-02-08 16:16:05

标签: asp.net-mvc razor asp.net-ajax viewmodel ajax.beginform

这已被多次询问,但我找不到令人满意的答案。我在类似的帖子上尝试了所有的建议,没有任何成功。

当我使用Ajax.BeginForm时,它不会将更新的值发布到控制器。它始终是默认的ViewModel。

 //Viewmodel
 public class MyViewModel
 {
      [Required]
      public int Prop1 {get; set;}
      [Required]
      public int Prop2 {get; set;}
 }

 //Controller action
 [HttpPost]
 public ActionResult Test(MyViewModel model)
 {
      return PartialView("_partialview");
 }

 //View
 @model Namespace.MyViewModel;

 @using(Ajax.BeginForm("Test", Model, new AjaxOptions()
 {
      UpdateTargetId = "divName",
      HttpMethod = "POST",
      InsertionMode = InsertionMode.Replace
 }))
 {
      @Html.TextBoxFor(x => x.Prop1)
      @Html.TextBoxFor(x => x.Prop2)
      <button type="submit">Submit</button>
 }

我尝试了多个建议,但控制器内的发布值始终是默认的MyViewModel,没有更新的值。

我还尝试了不同的变体来发布值:

@using(Ajax.BeginForm("Test", new { model }, new AjaxOptions()
@using(Ajax.BeginForm("Test", new { model = @Model }, new AjaxOptions()
@using(Ajax.BeginForm("Test", new { model = 
     new Namespace.MyViewModel{ Prop1 = @Model.Prop1, Prop2 = @Model.Prop2 } }, new AjaxOptions()

二手捆绑包:

"~/bundles/jqueryval"
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.unobtrusive-*"

我真的不知道还有什么可以尝试获取更新的值。

1 个答案:

答案 0 :(得分:0)

嗨,我用这个来解决这个问题:

put&#34; id&#34;在方法

public ActionResult StolenBadge(StolenDocViewModel id)
{
   var obj = id
   ... rest of code ...
}
Ajax Form中的

等于&#34; id = Model&#34;

  @model WFAccess.Models.ViewModels.StolenDocViewModel

  @using (Ajax.BeginForm("StolenBadge", "Requests", new AjaxOptions()
    {
        HttpMethod = "POST",
        UpdateTargetId = "document-body",
        InsertionMode = InsertionMode.Replace,
        LoadingElementId = "document-loading",
        OnBegin = "ClearBody('document-body')"
    }, new { id = Model }))
    {
        ... rest of form ...
    }
相关问题