【问题标题】:retain form values on error/exception保留错误/异常时的表单值
【发布时间】:2012-12-31 09:47:11
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 看起来这段代码会完全按照你现在的样子做。你觉得有什么不同?
  • 您也可以分享您的视图代码吗?看起来你那里的代码可以工作,除非你使用 Html.TextBox 而不是 Html.TextBoxFor
  • @AlexMoore 我用查看代码更新了我的问题
  • 对于您的ModelState.AddModelError('foo',...),您是否在这里使用 foo 作为占位符而不是实际代码中的任何内容?
  • @AlexMoore 我在我的真实代码中使用空字符串作为键。

标签: asp.net-mvc c#-4.0 asp.net-mvc-4 angularjs


【解决方案1】:

原来ng_model = "To" 属性是这个问题的原因。 ng-model 是 AngularJs 框架的内置指令,它处理客户端数据绑定,因为在这个范围内没有数据,所以我看不到任何数据。

您可以在这里阅读:ngModel

【讨论】:

    猜你喜欢
    • 2012-11-24
    • 2016-02-27
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多