2016-03-03 50 views
0

我想,當按鈕「創建」點擊和頁面是有效的它轉到下一個頁面。代碼如下:如何獲得一個按鈕以進入下一個頁面,如果頁面無效 - ASP.NET MVC

控制器

public ActionResult Create() 
     { 

      return View(new Charity()); 
     } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "ID,DisplayName,Date,Amount,TaxBonus,Comment")] Charity charity) 
    { 

     if (ModelState.IsValid) 
     { 
      db.Donations.Add(charity); 
      db.SaveChanges(); 
      return RedirectToAction("AdditionalInfo"); 
     } 
     return View(charity); 
    } 

表(_form)

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    <a href="~/Views/Charities/_Database2.cshtml">~/Views/Charities/_Database2.cshtml</a> 

    <div class="form-horizontal"> 
     <h4>Charity</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.DisplayName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.DisplayName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.DisplayName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @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.Amount, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.TaxBonus, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.TaxBonus, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.TaxBonus, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

創建視圖

@model CharitySite.Models.Charity 

@{ 
     ViewBag.Title = "Create"; 

} 

    <link href="@Url.Content("~/Content/Donate-Style.css")" rel="stylesheet" type="text/css" /> 

     <div> @Html.Partial("_Form", Model);</div> 

     <div> @Html.Action("_Database2");</div> @*Displays Table Table from Databa**@ 

     <div class="donatebtn"> 
      @Html.ActionLink("Donate", "Additionalinfo") 
     </div> 

     @section Scripts { 
      @Scripts.Render("~/bundles/jqueryval") 
     } 

我試圖在驗證完成後將它添加到控制器名稱「AdditionalInfo」中,並且用戶單擊創建按鈕,現在它驗證表單,但它不會在單擊創建按鈕時轉到下一個頁面。

回答

1

我試圖得到它

然後,你需要使用不同的過載電腦板命名爲「AdditionalInfo」。這:

return RedirectToAction("AdditionalInfo"); 

將用戶重定向到當前控制器上的AdditionalInfo行動。要進入不同的控制器,可以同時指定控制器和行動:

return RedirectToAction("SomeAction", "AdditionalInfo"); 
+1

更詳細地說明什麼大衛只是寫(這是正確的),這將不會保持剛發佈的信息的狀態。您必須將這些信息傳遞給您的下一個操作,然後返回頁面等,或者您可以將其存儲在緩存服務器或數據庫中。如果您確定您的應用程序永遠不會跨越多個服務器進行負載平衡,則還可以將其存儲在緩存或會話的內存中。 – ohiodoug

+0

@ohiodoug Thaks的答案是,信息存儲在數據庫中。 – John

+0

或者保存數據庫並使用RouteCollection重載傳遞它? – DPac

0

如果addiitonalInfo是一個控制器,你必須這樣調用它:

this.RedirectToAction("YourActionName","addiitonalInfo") 

你叫RedirectToAction的錯誤覆蓋方法和它尋找「addiitonalInfo」 actionName當前ccontroller

相關問題