我有一个 ASP.NET MVC 4 网页,在其中使用以下 HTML 代码为最终用户生成表单:
@using (Html.BeginForm("JEdit", "Post", FormMethod.Post, new { id = "frmEditPost" }))
{
<div id="postListEditor" class="regularContainer" style="position:relative;">
@Html.ValidationSummary(false)
@Html.HiddenFor(c=> c.Id, false)
<div class="floatLeft">
@Html.LabelFor(c => c.Title, true)
@Html.TextBoxFor(c => c.Title, new { @class = "tb1", @Style = "width:400px;" })
@Html.ValidationMessageFor(model => model.Title)
</div>
<br style="clear:both;" />
<div class="floatLeft">
@Html.LabelFor(c => c.Text, true)
@Html.TextAreaFor(c => c.Text, new { @class = "tb1", @Style = "width:400px; height:200px" })
@Html.ValidationMessageFor(model => model.Text)
</div>
<div style="clear:both;"></div>
@Html.Raw(@Html.SubmitButton("Posta", "btPost", "", "", "ValidateFormAndAjaxSubmit('frmEditPost', this);"))
</div>
}
单击提交按钮时,将运行以下 javascript:
function ValidateFormAndAjaxSubmit(formId, callingElement) {
if (IsNotDblClick(callingElement.id)) {
var _form = $("#" + formId);
var validator = _form.validate();
var anyError = false;
anyError = !_form.valid();
if (anyError) {
window.latestClick = '';
return false; // exit if any error found
}
$.post(_form.attr("action"), _form.serialize(), function (data) {
if (data.success && data.redirectUrl.length > 0) {
window.location.href = data.redirectUrl;
}
else {
var isValid = validateResponse(_form, data);
window.latestClick = '';
}
})
}
}
问题是 hiddenFor 永远不会被送回,但所有其他成员都是?我可以在生成的 HTML 中看到 hitten 字段设置如下:
<input data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="Id" name="Id" type="hidden" value="21">
那么为什么这个 hittenField 没有发送回服务呢?
Edit1: 这是根据 Chrome 中的 dev 发送的
Request URL:http://localhost:5215/Post/JEdit
Request Headersview source
Accept:*/*
Content-Type:application/x-www-form-urlencoded
Origin:http://localhost:5215
Referer:http://localhost:5215/Post/Edit/42
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
Id:42
Title:testar3532
Text:testas3532
Url:http://test.se
Tags:
编辑2:
这就是 ViewModel 的样子:
public class EditPostViewModel
{
public int Id = -1;
public EditPostViewModel() { }
public EditPostViewModel(string title, string text, string url)
{
Title = title;
Text = text;
Url = url;
}
[Display(Name = "Titel"/*Description = "..."*/)]
[StringLength(100, MinimumLength = 3, ErrorMessage = "...")]
[Required(ErrorMessage="...")]
public string Title { get; set; }
[Display(Name = "Text")]
[StringLength(2500, MinimumLength = 3, ErrorMessage = "...")]
public string Text { get; set; }
[StringLength(100, MinimumLength = 3, ErrorMessage = "...")]
[Display(Name = "Länk")]
[RegularExpression(@"^(http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?)?$", ErrorMessage="...")]
public string Url { get; set; }
[Display(Name = "Tags")]
[TagAttribute(ErrorMessage="...a")]
public string Tags { get; set; }
public List<int> TagIdList { get; set; }
public string CustomError { get; set; }
}