隐藏字段不发送到服务?

时间:2013-03-23 17:15:16

标签: c# asp.net-mvc submit hidden-field

我有一个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没有被送回服务?

编辑1: 这是根据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:

Edti2:

这就是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; }
    }

2 个答案:

答案 0 :(得分:2)

ASP.NET MVC默认情况下无法绑定到字段。只有属性。因此,请将Id成员更改为属性。

答案 1 :(得分:0)

尝试做@ Html.HiddenFor(c =&gt; c.Id,new {Value = Model.Id})