Javascript 使用ajax提交时在ASP.NETMVC的表单集合中获取空值。什么是问题?

Javascript 使用ajax提交时在ASP.NETMVC的表单集合中获取空值。什么是问题?,javascript,jquery,asp.net,ajax,asp.net-mvc,Javascript,Jquery,Asp.net,Ajax,Asp.net Mvc,我正在尝试使用ajax调用保存数据。但在实际结果中,我得到了模型的空值 HTML @using (Html.BeginForm("addaboutftw", "AboutFtw", FormMethod.Post, new {@class = "form-horizontal", role = "form", @id = "addaboutftw_frm", @name = "addaboutftw_frm" })) { <div class="form-group">

我正在尝试使用ajax调用保存数据。但在实际结果中,我得到了模型的空值

HTML

@using (Html.BeginForm("addaboutftw", "AboutFtw", FormMethod.Post, new {@class = "form-horizontal", role = "form", @id = "addaboutftw_frm", @name = "addaboutftw_frm" }))
{
    <div class="form-group">
        @Html.LabelFor(m => m.title, new { @for = "title", @class = "col-sm-2 control-label" })
        <div class="col-sm-8">
            @Html.TextBoxFor(m => m.title, new { @class = "form-control", @placeholder = "FTW from The Start" })
            @Html.HiddenFor(m => m.id, new { @class = "form-control",@placeholder = "FTW from The Start" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.detail_contents, new { @for = "detail_contents", @class = "col-sm-2 control-label" })

        <div class="col-sm-8">
            @Html.TextAreaFor(m => m.detail_contents, new { @class = "form-control", @placeholder = "Content in Detail" })
           <button type="button" class="btn btn-default" id="btn_save" name="btn_save">Save</button>
        </div>
    </div>
}
行动结果

[HttpPost]
public ActionResult addaboutftw(AboutFtwViewModel model)
  {
         int result=0;
         AboutFtw dbaboutftw = new AboutFtw();
         dbaboutftw.title = model.title;
         dbaboutftw.detail_contents = model.detail_contents ;
         result = _ftwCommonMethods.AboutFtwAdd(dbaboutftw);
         return Json(new { Message = result.ToString() });
 }
视图模型

public class AboutFtwViewModel{
   public Int64 id { get; set;}
   [DataType(DataType.Text)]
   [Display(Name = "Title")]
   public string title { get; set; }
   [DataType(DataType.Text)]
   [Display(Name = "Content in Detail")]
   public string detail_contents { get; set; }
}

什么是问题?我遗漏了什么吗?提前谢谢

代码运行良好。我认为您的Javascript中出现了一个语法错误,请按如下方式进行编辑:

$("#btn_save").click(function () {
    $.ajax({
        type: 'POST',
        url: "/Home/About",
        data: $("#addaboutftw_frm").serialize(),
        dataType: 'json',
        success: function (data) {
            alert(data.Message);
        }
    })
});
您错过了一个闭括号和分号更改:

public class AboutFtwViewModel{
  public Int64 id { get; set;}
  [DataType(DataType.Text)]
  [Display(Name = "Title")]
  public string title { get; set; }
  [DataType(DataType.Text)]
  [Display(Name = "Content in Detail")]
  public string detail_contents { get; set; }
}
致:


视图模型的命名与传递给控制器的模型不同。

为什么要使用
FormCollection
?将POST方法更改为
public ActionResult addabouttw(abouttw model)
,它将被正确绑定。为什么要添加
new{@for=“name”}
LabelFor()
方法已经为属性和数据中的内容添加了正确的
?而不是数据:$(“#addaboutftw_frm”).serialize()?这正是您现在所做的。但是,当您在POST方法中甚至没有使用它时,为什么要使用
@Html.HiddenFor(m=>m.id)
。为什么它从一开始就有
@placeholder=“FTW”
?(它是隐藏的!)为什么要为属性
name
生成控件,然后尝试将其分配给属性
title
?很抱歉,这段代码没有太多关于TFTW(关于TFTW模型)的合理添加,但仍然没有得到nll valuesSorry提供的信息。但在我的代码中,它们是存在的。但仍然是空的。
public class AboutFtwViewModel{
  public Int64 id { get; set;}
  [DataType(DataType.Text)]
  [Display(Name = "Title")]
  public string title { get; set; }
  [DataType(DataType.Text)]
  [Display(Name = "Content in Detail")]
  public string detail_contents { get; set; }
}
public class AboutFtw{
   public Int64 id { get; set;}
   [DataType(DataType.Text)]
   [Display(Name = "Title")]
   public string title { get; set; }
   [DataType(DataType.Text)]
   [Display(Name = "Content in Detail")]
   public string detail_contents { get; set; }
}