MVCプロジェクトのアクションメソッドの1つにデータをポストバックしようとしています。私は既に別の何かをするAjaxフォームを持っていますので、別のAjaxフォームを使用することはできません。だから私は$ .post関数を使うことに頼った。問題は、私のアクションメソッドが呼び出されると、私のモデルはnullです。
これが私の見解である:私はすべての不要なHTMLを削除
@model ActivityViewModel
@using (Ajax.BeginForm("Create", "Activities", new AjaxOptions() { UpdateTargetId = "panelContent", InsertionMode = InsertionMode.Replace }, new { id = "createactivity", autocomplete = "off" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.EmployeeId)
@Html.HiddenFor(m => m.IsAbscence)
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.ConflictIds)
@Html.HiddenFor(m => m.IsAbscence)
@Html.TextBoxFor(model => model.Date, "{0:dd.MM.yyyy}", new { @type = "date", @class = "ms-TextField-field", @autocomplete = "off" })
@if (!Model.IsAbscence)
{
@Html.HiddenFor(m => Model.Favorites, new { @id = "favoritehidden" })
@Html.HiddenFor(m => Model.Archive, new { @id = "archivehidden" })
<script type="text/javascript">
attachFabricCheckBoxHandler('#favoritehidden', '#favoritelabel');
attachFabricCheckBoxHandler('#archivehidden', '#archivelabel');
$(document).ready(function() {
$('#favoritelabel').click(function() {
var frm = $('#createactivity').serialize();
var token = $('[name=__RequestVerificationToken]').val();
$.post({
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: '/Activities/FilterProjects/',
data: { __RequestVerificationToken: token, model: frm.substring(frm.indexOf("&") + 1) },
statusCode: {
404: function (content) { showErrorDialog(content); },
500: function (content) { showErrorDialog(content); }
},
success: function (data) {
alert(data);
},
error: function (req, status, errorObj) {
showErrorDialog(status);
}
});
});
});
</script>
@Html.DropDownListFor(m => m.ProjectId, new SelectList(ViewBag.Projects, "Id", "ProjectDescription"), new { @class = "ms-Dropdown-select" })
}
@Html.TextBoxFor(model => model.StartTime, "{0:HH:mm}", new { @readonly = "readonly", @class = "ms-TextField-field", @placeholder = "Click to choose time" })
@Html.TextBoxFor(model => model.EndTime, "{0:HH:mm}", new { @readonly = "readonly", @class = "ms-TextField-field", @placeholder = "Click to choose time" })
@Html.TextAreaFor(model => model.Description, new { @class = "ms-TextField-field", @style = "height:100px; resize:vertical;" })
@if (!Model.IsAbscence)
{
@Html.TextAreaFor(model => model.Comment, new { @class = "ms-TextField-field", @style = "height:100px; resize:vertical;" })
}
}
お知らせ、構造は基本的に同じです。ここに私のViewModelは次のとおりです。
public class ActivityViewModel
{
public int Id { get; set; }
public DateTime Date { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Description { get; set; }
public int EmployeeId { get; set; }
public string ConflictIds { get; set; }
public string Comment { get; set; }
public int ProjectId { get; set; }
public bool IsAbscence { get; set; }
public bool Archive { get; set; }
public bool Favorites { get; set; }
}
私はこれを使用すると、私はいつも私のアクションメソッドにnullを取得:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult FilterProjects(ActivityViewModel model)
{
//When 'object model' is being passed, the serialized string and all the properties are there...
return PartialView("Create");
}
私は私に代わりに私の型付けのViewModelのオブジェクトを渡すとき、奇妙なことに、あります私は、この文字列から再インスタンス私のviewmodelでしたが、私は私の型指定されたモデルは、私の行動に渡されていた場合、それは非常に良くなり
EmployeeId=1&
IsAbscence=False&
Id=0&
ConflictIds=&
IsAbscence=False&
Date=27.04.2016&
Favorites=True&
Archive=False&
ProjectId=1&
StartTime=10%3A25& //Maybe these two values are screwing up the model?
EndTime=11%3A25&
Description=&
Comment=&
:アクションメソッドは、私はすべてのプロパティをシリアル化された文字列を取得します。あなたのアクションが呼び出されたときにnullでないように、フォームを適切にシリアル化するにはどうすればよいですか?私は "StartTime"と "EndTime"のプロパティを省略しようとしましたが、ここで干渉していると思ったので検証トークンの文字列を切り捨てましたが、明らかにそれは機能しませんでした。