Skip to main content
 首页 » 编程设计

c#之发送电子邮件后清除 MVC 中文本字段中的数据

2024年02月27日44del

我是 MVC 的初学者,在发送电子邮件后试图清除文本字段中的数据。我已成功发送电子邮件,但发送电子邮件后数据并未删除。我尝试了很多解决方案,但没有得到好的结果。那么,我该怎么办?

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

请您参考如下方法:

服务器端

发送邮件后添加这段代码

 ModelState.Clear();