During a recent operation, we will send the value of Textbox to Get via submit, and then we will try to remove the value entered in Textbox, but we will share the method we found when ViewBag is not working.
In MVC, there are times when you need to exchange a lot of data with a View.
In particular, you may need to pass a value after submit, or delete the value you entered before submit.
Normally, you can use ViewBag and ViewData.
ViewBag and ViewData can be used without difficulty.
However, there is a case where the existing input value does not disappear. ModelState.Clear () is a function that can be used in this case.
ModelState is a property of a Controller, and can be accessed from those classes inherit from System.Web.Mvc.Controller.
https://docs.microsoft.com/ko-kr/dotnet/api/system.web.mvc.controller.modelstate?view=aspnet-mvc-5.2
How to use ModelState
When ModelState is initialized, the current value and error value will be initialized.
This can be used to initialize input values
You can use the ViewBag and ViewData to pass the values you want to pass.
public ActionResult Index()
{
ModelState.Clear();
ViewBag.CurrentGroup = [Your want value here];
return View()
}
If you enter ModelState.Clear (); in the controller, the value and the name object will disappear.
After that, you can set the value to be transmitted newly by using ViewBag, etc., and initialize the value of TextBox.
The settings on the View page can be applied as follows:
<td>
@Html.TextBox("Group", ViewBag.CurrentGroup as string, new { placeholder = "input group" })
</td>
When set as above, the input value does not enter if ViewBag does not have a value, and if there is a value, the entered value is entered.
If you want to reset, ModelState.Clear(); will remove all previously entered values.
'.Net' 카테고리의 다른 글
The underlying connection was closed – REST API call over HTTPS (0) | 2020.11.04 |
---|---|
.Net for Apache Spark 1.0 공개 (0) | 2020.11.01 |
C# - DirectorySearcher search result more than 1000 (0) | 2020.11.01 |
.Net core에서 .Net 5로 변화, Core, Framework 통합 (0) | 2020.10.30 |
C# - DllImport – Using the C ++ DLL / Windows API (0) | 2020.10.29 |