I'm using Html.TextBoxFor for entering text and submit it later. What I want to get is that on submit, in case of exception/error, to return the user back to the form view with his previously entered value.
my POST action:
[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);
}
view code:
<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>
it turns out that the ng_model = "To" attribute is the reason for this problem.
ng-model is a built-in directive of AngularJs framework which handling client-side data-binding and because no data present at this scope I see no data.
You can read about is here: ngModel