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);
}
您應該在前端創建ViewModels。 –
爲什麼你需要通過ajax調用發送它,當你的頁面要提交時,如果你已經在控制器動作中傳遞了ViewModel,你已經設置了'HttpPost'控制器動作將會有你的值。你想要達到什麼目的?請詳細說明。 – Rohit416
如果一個視圖的屬性被修改之前發送給控制器我有一個彈出式(彈出式是/否),我想設置一個變量與彈出的結果 – user3024936