2016-07-31 88 views
0

您好我想要清除MVC5.I中使用ModelState.clear()在控制器中單擊提交按鈕後,所有字段。但是這隻能清除文本框的值。它沒有清除單選按鈕和下拉值。所以任何人都會告訴我這個問題的解決方案。通過單擊MVC5中的提交按鈕清除所有字段

我的視圖模型(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(); 
    } 

Advance Advance ..

+0

你好。你應該在你的問題中包含一段代碼。你很難得到任何迴應,沒有任何相關的代碼片段顯示你的問題。 – Christos

+0

現在檢查我的代碼Christos –

+0

可能重複的[Asp.net MVC ModelState.Clear](http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear) –

回答

0

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); 

} 
+0

不,我不想返回索引頁。我必須輸入很多條目。所以我無法點擊每次添加按鈕並輸入表單。所以只有我問這個明確的選項.. –

相關問題