2014-12-03 197 views
0

我的MVC表單將不會提交,除非我在我的TextBoxFor字段中有一個值。我正在使用TextBoxFor,因爲我確實需要存儲在模型屬性中的字段的值。我需要以下兩種解決方案之一,或者滿足我的需求。我需要提交表單以便能夠將常規文本框的值保存到模型屬性,或者當我的TextBoxFor爲空時允許提交。提交MVC空剃刀TextBoxFor字段

目前我的代碼看起來是這樣的:

@using (Ajax.BeginForm("PartialResults", "Students", new { LoadItemsOnly = true }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "find-results" })) 
{ 
<div class="inline-ct"> 
    <table> 
     <tr> 
      <td><span class="lbl">Name: </span></td> 
      @Html.TextBoxFor(m => m.Filter, new { autofocus = "autofocus", id = "name" }) 
     </tr> 
     <tr> 
      <td><span class="lbl">@Html.DisplayNameFor(m => m.LocationID): </span></td> 
      <td>@Html.DropDownListFor(m => m.LocationID, new SelectList(Model.Locations, "Key", "Value"))</td> 
     </tr> 
     <tr> 
      <td><span class="lbl">@Html.DisplayNameFor(m => m.ADGuid): </span></td> 
      <td>@Html.DropDownListFor(m => m.ADGuid, new SelectList(Model.Teachers, "Key", "Value"))</td> 
     </tr> 
     <tr> 
      <td><span class="lbl">@Html.DisplayNameFor(m => m.ClassCode): </span></td> 
      <td>@Html.DropDownListFor(m => m.ClassCode, new SelectList(Model.SchoolClasses, "Key", "Value"))</td> 
     </tr> 
     <tr> 
      <td><button type="submit" class="btn-primary" id="btn-find">Find</button></td> 
      <td></td> 
     </tr> 
    </table> 
</div> 

<div id="student-find-results" class="row"> 
</div> 
} 

如前所述時,有一個值TextBoxFor這工作得很好,但形式是不是如果是空的提交。任何幫助將不勝感激。

注意: 我試圖添加一個onclick甚至到按鈕,但它不提交給控制器。

+3

請看看你的模型,看看[必填]數據註釋。爲什麼你改變過濾器 – Amila 2014-12-03 16:07:55

+0

的'id'哇......我的臉有點紅。修復它,謝謝。如果你把這個作爲答案,我會標記爲這樣。感謝您的幫助! – mgrenier 2014-12-03 16:10:56

回答

1

首先,你傳遞給視圖中的所有檢查,如果你不是在你的ViewModel對象將在球場上[必需]屬性:

[Required(ErrorMessage = "")] 
public string MyAttribute { get; set; } 

二,請確保您的文本框字段可以使用?關鍵字在你的ViewModel那樣

public int? MyAttribute { get; set; } 

第三,檢查是否有客戶端上的字段的JQuery驗證。 注意,如果要禁用驗證您的所有字段,您可以通過刪除的ModelState檢查修改您的控制器不ModelState中進行測試:如果標有'

if(ModelState.IsValid) 
{ 
// TODO: 
} 
+1

感謝您提供完整的答案,以幫助其他誰的問題可能會稍微不同,那麼我的。 – mgrenier 2014-12-03 16:31:16