Nabia Saroosh Nabia Saroosh - 3 years ago 150
ASP.NET (C#) Question

After sending the email clear the data from text fields in MVC

I am beginner in MVC and trying to clear the data from text fields after sending the email. I have sent the email successfully but data is not removing after sending the email. I have tried many solutions, but I am not getting the good results. So, what should I do?

Create.cshtml

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group" id="name">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control input-lg" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>

<div class="form-group" id="email">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control input-lg" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>

<div class="form-group" id="comment">
@Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control input-lg comments" } })
@Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
</div>

<div class="form-group">
<input id="success" type="submit" value="Get In Touch" class="btn btn-primary btn-block btn-lg btn-login" />
@*<a class="btn btn-primary btn-block btn-lg" onclick="SendEmail()" >click to send email</a>*@
</div>


FeedbacksController.cs

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Comments")] Feedback feedback)
{
if (ModelState.IsValid)
{
bool result = false;
db.Feedbacks.Add(feedback);
db.SaveChanges();
result = SendEmail("abc@gmail.com", "Feedback", "<p>Hi Admin,<br/>My name is "+ feedback.Name + ". <br/> E_mail ID: " + feedback.Email + "<br/><br/>" + feedback.Comments + "<br/>Kind Regards,<br/>" + feedback.Name + "</p>");

return View( );
}
return View(feedback);
}

Answer Source

Server side

Add this code after send email in action

 ModelState.Clear();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download