【问题标题】:Clear all fields by clicking submit button in MVC5通过单击 MVC5 中的提交按钮清除所有字段
【发布时间】:2016-12-05 19:08:45
【问题描述】:

嗨,我想在 MVC5 中单击提交按钮后清除所有字段。我在控制器中使用了 ModelState.clear()。但这只会清除文本框的值。它没有清除单选按钮和下拉值。所以有人告诉我这个问题的解决方案。

我的 ViewModel(CustomerViewModel)

    public System.Guid CustomerID { get; set; }
    [Required(ErrorMessage = " Enter The Customer Name.")]
    [StringLength(100, ErrorMessage = "Name Only Accepted 100 Characters.")]
    public string CustomerName { get; set; }
    public System.Guid CustomerTypeID { get; set; }
    [Required(ErrorMessage = "CustomerType Required.")]
    public string CustomerType { get; set; 
    public System.Guid AddressID { get; set; }
    public string Address { get; set; }
    public string Street { get; set; }
    public string Location { get; set; }
    public string Place { get; set; }
    public Nullable<System.Guid> AreaID { get; set; }
    public string Area { get; set; }
    public Nullable<System.Guid> CityID{ get; set; }
    public string  City { get; set; }
    public string PinCode { get; set; }

我的观点

  @Html.Label("Customer Name", new { @class = "control-label" })
  @Html.ValidationMessageFor(model => model.CustomerName)
  @Html.TextBoxFor(model => model.CustomerName, new { @class = "form-control required", type = "text" })


   @Html.Label("Customer Type", new { @class = "control-label" })
   @Html.DropDownList("CustomerTypeID", null, "Select", new { @class = "form-control required" })


   @Html.LabelFor(model => model.Street, new { @class = "control-label" })
   @Html.TextBoxFor(model => model.Street, new { @class = "form-control", type = "text required" })
   @Html.ValidationMessageFor(model => model.Street)


   @Html.LabelFor(model => model.Location, new { @class = "control-label" })
   @Html.TextBoxFor(model => model.Location, new { @class = "form-control", type = "text " })
   @Html.ValidationMessageFor(model => model.Location)


    @Html.LabelFor(model => model.Place, new { @class = "control-label" })
    @Html.TextBoxFor(model => model.Place, new { @class = "form-control", type = "text" })
    @Html.ValidationMessageFor(model => model.Place)


    @Html.LabelFor(model => model.Area, new { @class = "control-label" })
    @Html.DropDownList("AreaID", null, "Select", new { @class = "form-control required" })

    @Html.LabelFor(model => model.PinCode, new { @class = "control-label" })
    @Html.TextBoxFor(model => model.PinCode, new { @class = "form-control", type = "text" })
    @Html.ValidationMessageFor(model => model.PinCode)

我的控制器

    public ActionResult Create()
    {
        return View();
    } 

   [HttpPost]

   Public ActionResult Create(CustomerViewMode CVM)
   {

   var Customerobj = new Customer
   {
    CustomerID= Guid.NewGuid(),
    CustomerName= CVM.CustomerName;
    // remaining saving code

   };

    db.Customers.Add(Customerobj);
     ModelState.Clear();
        return View();
    }

提前谢谢..

【问题讨论】:

  • 你好。您应该在问题中包含一段代码。如果没有任何相关代码的 sn-p 显示您的问题,您很难得到任何响应。
  • 现在检查我的代码 Christos
  • 好的,我有一张支票

标签: c# asp.net-mvc-5


【解决方案1】:

ModelState.clear() 用于清除 ModelState 字典中的错误或用户设置的任何验证。您可以使用以下代码,在 POST 方法中重定向到 GET 方法并初始化新模型。

 [HttpGet]
public ActionResult Index()
{
    AdminModel model = new AdminModel();
    return View(model);
}
[HttpPost]
public ActionResult Index(AdminModel model,FormCollection c)
{
    if (ModelState.IsValid)
    {
       return RedirectToAction("Index", new { Controller = "Sample1" });
    }

        return View(model);

}

【讨论】:

  • 不,我不想返回索引页面。我必须放很多条目。所以我不能每次都点击添加按钮并输入表格。所以只有我问这个明确的选项..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 2020-09-03
  • 2012-08-07
  • 2011-08-10
  • 2011-10-08
相关资源
最近更新 更多