【问题标题】:Prevent MVC Form to clear form values防止 MVC 表单清除表单值
【发布时间】:2015-04-05 01:54:32
【问题描述】:

所以我的问题很简单。提交我的表单后,所有表单值都将被删除。

查看 Stackoverflow 上提出的问题让我觉得大多数人都有相反的问题,这是默认行为?

反正我的观点很简单

@using (Html.BeginForm("Index", "Default"))
{
  <input type="text" name ="FirstName" />
  <input type="text" name="LastName" />
  <input type="submit" value="Send" />
}

这里是控制器。

 public class DefaultController : Controller
 {
    // GET: Default
    public ActionResult Index()
    {
        var model = new FormViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(FormViewModel model)
    {
        return View(model);
    }
}

我的视图模型

public class FormViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

提交后我的输入字段为空。如何在输入字段中保留值? (在现实生活中我当然有更大的形式)

【问题讨论】:

  • 使用 Ajax.BeginForm 而不是 Html.BeginForm
  • 不会改变任何东西。仍然是相同的行为。

标签: c# asp.net-mvc forms razor postback


【解决方案1】:

使用 HtmlHelpers 文本框

Html.TextBox("FirstName", Model.FirstName, new {
    @class = "form-control",
    PlaceHolder = "FirstName"
})

创建模型以将数据抓取到控制器中,并在渲染视图时传递模型

【讨论】:

    【解决方案2】:

    再次调用视图,再次转发数据,和第一次一样。返回视图(数据);其中数据属于某种类型(最好是 ViewModel),并且填充了已发布的表单数据(预期来自数据绑定)。

    这是经典:

        public ActionResult New ()
        {
            return View();
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult New (NewPlaceVM vm)
        {
            if (ModelState.IsValid && vm != null)
            {
                var model = Mapper.Map<NewPlaceVM, Place>(vm);
                _place.New(model);
                return RedirectToAction("Index");
            }
            return View(vm);
        }
    

    在 View 中使用 EditorFor 或任何其他最适合的 HTML 助手

    @model NewPlaceVM
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Name, null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Ignore, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Ignore, null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Ignore, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10 text-right">
                    <input type="submit" value="@Ress.Ress.Create" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
    

    【讨论】:

    • @Tushar Gupta 我没有看到视图问题
    • 添加@Html.TextBoxFor(model => model.LastName);帮助。我希望我没有那样做,但似乎没有办法。
    • 你可以这样做 它会做同样的事情,但使用 HTML 助手更聪明
    【解决方案3】:
    I have prevented form clearing for search form for my website mrnams.com
    
    View
            @using (@Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "navbar-form navbar-right pull-right" }))
            {
                <div class="input-group">
                    <input id="input-searchQuery" type="text" class="form-control" placeholder="Search this site" name="q">
                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-default">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </span>
                </div>
            }
    
    jQuery functions in View 
    
    @section scripts{
    <script type="text/javascript">
            $(function ()
            {
                var queryReturned = '@ViewBag.SearchQuery';
                $("#input-searchQuery").val(queryReturned); 
            });
     
    
    </script>
    }
     
    And here is the controller. 
    
    public class Home : Controller
            {            
    public ActionResult Search(string q)
                {
                    ViewBag.SearchQuery = q;
    }
    }
    

    演示访问 https://mrnams.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多