2013-01-16 14 views
0

我使用Html.TextBoxFor來輸入文本並在稍後提交。 我想得到的是,在提交時,如果發生異常/錯誤,將用戶返回到表單視圖,並且以前輸入的值爲保留錯誤/異常的表格值

我POST操作:

[HttpPost] 
public ActionResults Add(Model model) 
{ 
    try 
    { 
     //on succeed 
     _repository.Add(model); 
     return RedirectToAction("ThankYou", "Home"); 
    } 
    catch(Exception ex) 
    { 
     ModelState.AddModelError('foo', ex.Message); 
    } 

    return View(model); 
} 

視圖代碼:

<div class="origin"> 
       <div class="float-left margin-right"> 
        @Html.LabelFor(m => m.From, "From:", new { @class = "block-display" }) 
        @Html.TextBoxFor(m => m.From, new 
         { 
          type = "text", 
          @class = "airport-auto-complete", 
          auto_complete = "", 
          placeholder = "enter place or airport", 
          ng_model = "From" 
         }) 
       </div> 
       <div class="float-left"> 
        @Html.LabelFor(m => m.DepartAt, "Depart:", new { @class = "block-display" }) 
        @Html.TextBoxFor(m => m.DepartAt, new 
         { 
          type = "text", 
          @class = "begin-trip-date date-input", 
          ng_model = "DepartAt" 
         }) 
        <input type="button" class="more-options-btn" value="More.." /> 
        <input type="button" class="transport-btn" /> 
       </div> 
       <br style="clear: left;" /> 
      </div> 
      <div class="more-details"> 
       <div> 
        <label> 
         <input type="checkbox" id="allow-via" /> 
         Allow nearby</label> 
        <br /> 
       </div> 
       <div> 
        <label for="via" class="block-display">Via:</label><input type="text" id="via" auto-complete /> 
       </div> 
       <div> 
        <div class="float-left left"> 
         <label for="flight-num" class="block-display">Flight No.:</label><input type="text" id="flight-num" /> 
        </div> 
        <div class="float-left"> 
         <label for="stops" class="block-display">Stops:</label><input type="text" id="stops" /> 
        </div> 
       </div> 
      </div> 
      <div class="destination"> 
       <div class="float-left margin-right"> 
        @Html.LabelFor(m => m.To, "To:", new { @class = "block-display" }) 
        @Html.TextBoxFor(m => m.To, new 
       { 
        type = "text", 
        @class = "airport-auto-complete", 
        auto_complete = "", 
        placeholder = "enter place or airport", 
        ng_model = "To" 
       }) 
       </div> 
       <div class="float-left"> 
        @Html.LabelFor(m => m.ReturnAt, "Return:", new { @class = "block-display" }) 
        @Html.TextBoxFor(m => m.ReturnAt, new 
       { 
        type = "text", 
        @class = "end-trip-date date-input", 
        ng_model = "ReturnAt" 
       }) 
       </div> 
       <br style="clear: left;" /> 
      </div> 
+1

這似乎是該代碼會做你想要什麼,因爲它是現在。你看到的是什麼不同? – Dismissile

+1

你能分享你的視圖代碼嗎?它看起來像你在那裏的代碼將工作,除非你使用Html.TextBox而不是Html.TextBoxFor –

+0

@AlexMoore我更新了我的問題與查看代碼 –

回答

0

事實證明,該ng_model = "To"屬性是這個問題的原因。 ng-model是處理客戶端數據綁定的AngularJs框架的內置指令,因爲沒有數據存在於此範圍內,所以我看不到任何數據。

您可以瞭解是在這裏:ngModel