2016-12-30 36 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的做法。例如:如果用戶從聯繫人頁面提供數據,那麼將它們重定向回聯繫人頁面而不是某個其他頁面會很好並且容易理解。這需要很多警告,特別是在重命名視圖等

希望這可以幫助你。

相關問題