C# 发送电子邮件后,清除MVC中文本字段中的数据
我是MVC的初学者,在发送电子邮件后试图清除文本字段中的数据。我已成功发送电子邮件,但发送电子邮件后数据未被删除。我尝试了许多解决方案,但没有得到好的结果。那么,我该怎么办 Create.cshtmlC# 发送电子邮件后,清除MVC中文本字段中的数据,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我是MVC的初学者,在发送电子邮件后试图清除文本字段中的数据。我已成功发送电子邮件,但发送电子邮件后数据未被删除。我尝试了许多解决方案,但没有得到好的结果。那么,我该怎么办 Create.cshtml @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> @Html.ValidationSummary(true, "", new { @cl
@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>
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control input lg”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control input lg”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Comments,htmlAttributes:new{@class=“control label”})
@EditorFor(model=>model.Comments,new{htmlAttributes=new{@class=“form control input lg Comments”})
@Html.ValidationMessageFor(model=>model.Comments,“,new{@class=“text danger”})
@*单击以发送电子邮件*@
反馈控制器.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);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果创建([Bind(Include=“Id,Name,Email,Comments”)]反馈)
{
if(ModelState.IsValid)
{
布尔结果=假;
db.Feedbacks.Add(反馈);
db.SaveChanges();
结果=发送电子邮件(“abc@gmail.com“,”反馈“,”你好,管理员,
我的名字是“+Feedback.name+”
电子邮件ID:“+Feedback.Email+”
“+Feedback.Comments+”
,
“+Feedback.name+””;
返回视图();
}
返回视图(反馈);
}
服务器端
在发送电子邮件后添加此代码
ModelState.Clear();
遵循PRG模式并使用
返回重定向到操作(“创建”);
(而不是返回视图();
)可以在返回前重置模型属性view@SouvikGhosh我该怎么做?可能您的模型是反馈
。因此您需要设置Feedback.Name=“”;Feedback.Email=“”;Feedback.Comments=“”
然后返回视图(反馈);
@SouvikGhosh,这不会起任何作用。因此,如果电子邮件未成功发送,表单字段将被重置。@NabiaSaroosh在返回视图()之前添加ModelState.Clear();