2016-12-30 37 views
0

我有一个页面,它有一个窗体,所有东西都能正常工作。 我的问题是,当我的模型状态是无效的,那么如何恢复与我们联系,索引视图不创建视图如何使用ASP.net将对象传递到特定视图MVC

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

在这种情况下,如果模型状态无效,它看起来脱颖而出创造的看法,但我不我想创建一个视图。我只想带我们联系索引页面。

这是我的索引视图

 @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> 
     } 

感谢所有帮助

回答

2

如果你只是想调用一个视图,并通过在正确的模型,那么你可以使用重载与视图名称如下情况:

与模型的负荷视图:无模型

return View("Index", contact_US); //Index is View name and contact_US is the Model. 

加载视图:

return View("Index"); 

确保视图中预期的模型以及您从动作传递的内容匹配。

理想情况下,除了一些特定的情况下更好地保持接近MVC的做法。例如:如果用户从联系人页面提供数据,那么将它们重定向回联系人页面而不是某个其他页面会很好并且容易理解。这需要很多警告,特别是在重命名视图等

希望这可以帮助你。

相关问题