Asp.net mvc 如何使用ASP.net MVC将对象传递到特定视图
我有一个页面,其中有一个表单,所有内容都正常工作。 我的问题是,当我的模型状态无效时,如何将“联系我们”返回到“索引视图”而不是“创建视图”Asp.net mvc 如何使用ASP.net MVC将对象传递到特定视图,asp.net-mvc,Asp.net Mvc,我有一个页面,其中有一个表单,所有内容都正常工作。 我的问题是,当我的模型状态无效时,如何将“联系我们”返回到“索引视图”而不是“创建视图” [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US) { if (ModelState.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
{
if (ModelState.IsValid)
{
db.Contact_US.Add(contact_US);
db.SaveChanges();
MailMessage mail = new MailMessage();
mail.To.Add("");
mail.From = new MailAddress("");
mail.Subject = contact_US.Email;
string body = contact_US.Message;
mail.Body = "Phone:" + contact_US.Phone + "<br />Name:" + contact_US.Name +"<br />Email:"+contact_US.Email+ "<br /><br />" + body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("", "");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
TempData["ResultMessage"] = "Thank you for your enquiry and or enrolment. we will attend to this submission and come back to you soon";
TempData["TimeMessage"] = "Your entry was received on"+DateTime.Now;
return RedirectToAction("Index");
}
return View(contact_US);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果创建([Bind(Include=“Id,Name,Email,Phone,Message,Date”)]Contact\u US Contact\u US)
{
if(ModelState.IsValid)
{
db.联系我们。添加(联系我们);
db.SaveChanges();
MailMessage mail=新的MailMessage();
mail.To.Add(“”);
mail.From=新邮件地址(“”);
mail.Subject=contact_US.Email;
字符串体=contact_US.Message;
mail.Body=“电话:”+contact\u-US.Phone+”
姓名:“+contact\u-US.Name+”
电子邮件:“+contact\u-US.Email+”
”+Body;
mail.IsBodyHtml=true;
SmtpClient smtp=新SmtpClient();
smtp.Host=“smtp.gmail.com”;
smtp.Port=587;
smtp.UseDefaultCredentials=false;
smtp.Credentials=new System.Net.NetworkCredential
(“”,“”);//输入seders用户名和密码
smtp.EnableSsl=true;
smtp.发送(邮件);
TempData[“ResultMessage”]=“感谢您的查询和/或报名。我们将处理此提交并很快回复您”;
TempData[“TimeMessage”]=“您的输入是在”+DateTime.Now收到的;
返回操作(“索引”);
}
返回视图(联系我们);
}
在这种情况下,如果模型状态无效,它看起来是在创建视图,但我不想创建视图。我只想把我们联系到索引页
这是我的索引视图
@using (Html.BeginForm("Create", "Contact_Us", FormMethod.Post, new { @id = "contactusform" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group hidden">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12 contactusmsg">
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group contactuspostbtn">
<div class="col-md-12">
<input id="postcontactusmessage" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
}
@使用(Html.BeginForm(“创建”,“联系我们”,FormMethod.Post,new{@id=“contactusform”}))
{
@Html.AntiForgeryToken()
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2txtformat”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2txtformat”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Phone,htmlAttributes:new{@class=“controllabel col-md-2txtformat”})
@EditorFor(model=>model.Phone,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Phone,“,new{@class=“text danger”})
@LabelFor(model=>model.Date,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Date,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Date,“,new{@class=“text danger”})
@LabelFor(model=>model.Message,htmlAttributes:new{@class=“controllabel col-md-2txtformat”})
@Html.TextAreaFor(model=>model.Message,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Message,“,new{@class=“text danger”})
}
感谢您的帮助如果您只想调用一个视图并传入正确的模型,那么您可以在视图名称中使用重载,如下所示: 带有模型的负载视图:
return View("Index", contact_US); //Index is View name and contact_US is the Model.
不带模型的负载视图:
return View("Index");
确保视图期望的模型与您从操作传递的内容匹配
理想情况下,除了某些特定情况外,最好保持接近MVC实践。例:若用户从联系人页面提供数据,那个么将它们重定向回联系人页面而不是其他页面将是很好的,也很容易理解的。这需要特别小心,尤其是在重命名视图等时
希望这对您有所帮助。如果您只想调用一个视图并传入正确的模型,那么您可以在视图名称中使用重载,如下所示: 带有模型的负载视图:
return View("Index", contact_US); //Index is View name and contact_US is the Model.
不带模型的负载视图:
return View("Index");
确保视图期望的模型与您从操作传递的内容匹配
理想情况下,除了某些特定情况外,最好保持接近MVC实践。例:若用户从联系人页面提供数据,那个么将它们重定向回联系人页面而不是其他页面将是很好的,也很容易理解的。这需要特别小心,尤其是在重命名视图等时
希望这对你有帮助