简体   繁体   中英

Is there a way to clear an @Html.TextAreaFor

I am using asp.net core razor engine. After I enter text in my TextArea and hit the submit button I want to clear the text in the TexArea. I have not been able to find anything on how to do this using html or c#. Here is my code for the TextAreaFor

 <h1>Add Your Comment</h1>
                @using(Html.BeginForm("AddComment","Home"))
                {
                    <p>
                        <label>Your Comment</label>
                        @Html.TextAreaFor(d=>d.comment)
                        @Html.ValidationMessageFor(d => d.comment)
                    </p>

                    <input type="submit" name="submit" value="Add my Comment!"/>
                }

Here is my controller for AddComment

[HttpPost]
        [Route("addComment")]
        public IActionResult AddComment(Messages model)
        {  
            var user_id = HttpContext.Session.GetString("Id");
            if(user_id == null){
                return RedirectToAction("Index");
            }
            model.Users_id = Convert.ToInt32(user_id.ToString());
            userFactory.addComment(model, user_id);
            setTempData();

            ViewBag.Messages = userFactory.FindAll();
            model.comment ="";
            return View("Login", model);  
        }

The calls for userFactory work, it is a call to my factory that talks to my db

After all the work is done, you should not return View(...) , but rather redirect the user to it with RedirectToAction . By doing that, you clear out all form fields, and make use of the Post-redirect-get pattern , which stops your users from double-submitting.

The same pattern should nearly always be applied when you post an entire page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM