2013-03-23 58 views
0

我有一個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瀏覽器發送

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:

這是視圖模型的樣子:

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; } 
    } 
+0

使用Fiddler來驗證它確實沒有被髮送。 – usr 2013-03-23 17:18:03

+0

爲什麼你傳遞'false'作爲第二個參數?如果有什麼應該是HTML屬性的匿名對象。如果你不需要額外的屬性,不要傳遞第二個參數。 – rossipedia 2013-03-23 17:20:25

+0

@Bryan,這只是一個測試,如果我刪除它,仍然不會有任何ID發回服務。 – Banshee 2013-03-23 17:27:31

回答

2

默認情況下,ASP.NET MVC無法綁定到字段。只有屬性。因此請將您的Id會員更改爲屬性。

+1

非常感謝您的幫助! – Banshee 2013-03-23 18:49:46

0

嘗試做@ Html.HiddenFor(c => c.Id,new {Value = Model.Id})

+0

抱歉不工作。 – Banshee 2013-03-23 17:40:22

+0

如果你的hiddeninput已經有一個id,或者你插入一個新的記錄? – Andrew 2013-03-23 17:45:34

相關問題