2013-11-23 119 views
1

我試圖通過使用Ajax調用將模型發送到控制器。問題是在Controller中視圖的所有屬性都是空的。使用Ajax調用將模型從視圖傳遞到控制器

我的視圖模型爲:

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

和在控制器:

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

您應該在前端創建ViewModels。 –

+0

爲什麼你需要通過ajax調用發送它,當你的頁面要提交時,如果你已經在控制器動作中傳遞了ViewModel,你已經設置了'HttpPost'控制器動作將會有你的值。你想要達到什麼目的?請詳細說明。 – Rohit416

+0

如果一個視圖的屬性被修改之前發送給控制器我有一個彈出式(彈出式是/否),我想設置一個變量與彈出的結果 – user3024936

回答

-1

在AJAX調用您要發送的數據爲字符串:

data: JSON.stringify(check), 

你必須提供一個名字在AJAX調用的對象,它應該相同目標控制器的動作方法的參數名稱。並且不需要在客戶端綁定JSON對象。因此,它應該是這樣的:

data: { myObject1: check}, 

因爲你是從客戶端發送JSON這樣的操作方法應該是這樣的:

public ActionResult Save(string myObject1) 

內。然後採取行動,你可以反序列化JSON對象,並創建模型或任何處理您的需要。

相關問題