阻止MVC表单清除表单值

时间:2015-02-05 09:14:45

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

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

查看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);
    }
}

我的ViewModel

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

}

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

3 个答案:

答案 0 :(得分:0)

Whan再次调用视图,再次转发数据,与第一次相同。返回视图(数据);其中数据属于某种类型(最好是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>
}

答案 1 :(得分:0)

使用HtmlHelpers文本框

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

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

答案 2 :(得分:0)

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/