使用Ajax调用将Model从视图传递到控制器

时间:2013-11-23 13:28:47

标签: jquery asp.net-mvc-4

我尝试使用Ajax调用将模型发送到控制器。问题是在Controller中,视图的所有属性都为null。

我的viewModel是:

public class ActivityDetailsViewModel
    {
    public ActivityDto Activity { get; set; }
    public string ClientName { get; set; }
    public string ProjectName { get; set; }
    public string Parent { get; set; }
    public int? ParentId { get; set; }
    public int? ProjectId { get; set; }
    public bool UpdateFinancialDataRight { get; set; }
    public int? Level { get; set; }
    public bool HasViewFinancialDataRight
    {
        get
        {
            return HttpContext.Current.User.IsInRole(UserRole.ViewFinancialData) ||
                   HttpContext.Current.User.IsInRole(UserRole.UpdateActivityFinancialData);
        }
    }
    public bool HasUpdateFinancialDataRight
    {
        get
        {
            return HttpContext.Current.User.IsInRole(UserRole.UpdateActivityFinancialData);
        }
    }
    public bool IsInEdit { get; set; }
    public ActivityDetailsViewModel()
    {

    }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public ActivityDetailsViewModel(ActivityDto activity,int?parentId,int?projectId, string clientName, string projectName, string parent, bool updateFinancialDataRight,int? level,bool isInEdit)
    {
        this.Activity = activity;
        this.ClientName = clientName;
        this.ProjectName = projectName;
        this.Parent = parent;
        this.ParentId = parentId;
        this.ProjectId = projectId;
        this.Level = level;
        this.IsInEdit = isInEdit;
        StartDate = Utils.DateUtils.IntToDate(activity.StartDate);
        EndDate = Utils.DateUtils.IntToDate(activity.EndDate);
        UpdateFinancialDataRight = updateFinancialDataRight;
    }
}

在我看来,我有以下内容:

using (Ajax.BeginForm("Save", "Activities", new { activityId = @Model.Activity.Id }, new AjaxOptions() { OnBegin = "beforeSaving('" + @Model.Activity.Id + "')", OnSuccess = "handleSuccess" }))
              {
                  <input id="btnRun" type="submit"  value="submit" />   

                     @Html.TextBoxFor(m => m.ParentId, new { @class = "hiddenFiled" })
                    @Html.TextBoxFor(m => m.ProjectId, new { @class = "hiddenFiled" })
                   @Html.TextBoxFor(m => m.Level, new { @class = "hiddenFiled" })

......... }

ajax电话:

$("#btnRun").click(function (e) {
    debugger;
    e.preventDefault();
    var check = '@Html.Raw(Json.Encode(Model))';
    $.ajax({
        url: 'Activities/Save',
        type: 'POST',
        data: JSON.stringify(check),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        }
    });       
});

并在Controller中:

[HttpPost]
    public ActionResult Save(ActivityDetailsViewModel view)
    {  
        return PartialView("ActivitiesDetailsWindow", view);
    }

1 个答案:

答案 0 :(得分:-1)

在AJAX调用中,您将以字符串形式发送数据:

data: JSON.stringify(check),

您必须在AJAX调用中为object提供一个名称,它应该与目标控制器操作方法中的参数名称相同。并且不需要在客户端使用stringfy JSON对象。所以它应该是这样的:

data: { myObject1: check},

并且由于您是从客户端发送JSON所以操作方法应该是:

 public ActionResult Save(string myObject1 ) 

然后在内部操作中,您可以反序列化JSON对象并创建模型或您需要的任何处理。