2010-10-30 48 views
0

我試着在asp.net mvc2上使用mvc2模板並使用它。在模板之後,我創建了我自己的模型,控制器和視圖,並在其中分別存放了文件夾位置我還爲此控制器和視圖更改了global.asax中的默認路由。現在我的視圖正在被加載,但是當我點擊我的視圖中的按鈕時,我的控制器中寫入的方法都沒有被擊中。可能是什麼原因,我錯過了什麼?我還想在我的視圖呈現之前從我的控制器調用預加載方法。請幫助......卡住了。 這裏是我的看法ASP.Net MVC 2 - 按鈕點擊視圖不調用控制器中的任何方法?我錯過了什麼?

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CreditCashAllocationSystem.Models.ConfigurationModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Configuration 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>Configuration</h2> 

    <% using (Html.BeginForm()) {%> 
     <%= Html.ValidationSummary(true) %> 

     <fieldset> 
      <legend>Fields</legend> 

      <div class="editor-label"> 
       <%= Html.LabelFor(model => model.DropOffDaysForward) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.DropOffDaysForward) %> 
       <%= Html.ValidationMessageFor(model => model.DropOffDaysForward) %> 
      </div> 

      <div class="editor-label"> 
       <%= Html.LabelFor(model => model.DropOffDaysBackward) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.DropOffDaysBackward) %> 
       <%= Html.ValidationMessageFor(model => model.DropOffDaysBackward) %> 
      </div> 

      <div class="editor-label"> 
       <%= Html.LabelFor(model => model.DealDropOffDateDays) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.DealDropOffDateDays) %> 
       <%= Html.ValidationMessageFor(model => model.DealDropOffDateDays) %> 
      </div> 

      <div class="editor-label"> 
       <%= Html.LabelFor(model => model.DealHistoryDays) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.DealHistoryDays) %> 
       <%= Html.ValidationMessageFor(model => model.DealHistoryDays) %> 
      </div> 

      <div class="editor-label"> 
       <%= Html.LabelFor(model => model.UnappliedHistoryDays) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.UnappliedHistoryDays) %> 
       <%= Html.ValidationMessageFor(model => model.UnappliedHistoryDays) %> 
      </div> 

      <p> 
       <input type="submit" value="Create" /> 
      </p> 
     </fieldset> 

    <% } %> 

    <div> 
     <%= Html.ActionLink("Back to List", "Index") %> 
    </div> 

</asp:Content> 

,這裏是我的控制器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace CreditCashAllocationSystem.Controllers 
{ 
    public class ConfigurationController : Controller 
    { 

     // 
     // GET: /Configuration/Create 

     //will be called on Form Load 
     public ActionResult Create() 
     { 
      return View("Configuration"); 
     } 

     // 
     // POST: /Configuration/Create 


     //Method will be called once u click on create/save button 
     [HttpPost] 
     public ActionResult Create(FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add insert logic here 

       return View("Configuration"); 
      } 
      catch 
      { 
       return View("Configuration"); 
      } 
     } 
    } 
} 

這裏是我的Global.asax:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace CreditCashAllocationSystem 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Configuration", action = "Create", id = UrlParameter.Optional } // Parameter defaults 
      ); 
     } 

     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      RegisterRoutes(RouteTable.Routes); 
     } 
    } 
} 

請幫幫忙,我失去了什麼?

回答

0

這個方法應該得到調用的時候,你按一下按鈕,但所有你正在做的是要馬上回相同的觀點:

[HttpPost] 
public ActionResult Create(FormCollection collection) 
{ 
    try 
    { 
     // TODO: Add insert logic here 
     //Seriously, add some code here, or all you do is go back to the same view. 
     return View("Configuration"); 
    } 
    catch 
    { 
     return View("Configuration"); 
    } 
} 
+0

做一些斷點調試可以幫助證實了這一點。 – xandy 2010-10-30 15:45:06

1

你需要指定,當你調用BeginForm控制器和行動

<% using(Html.BeginForm("Create", "Configuration")) {%> 
+0

謝謝。它現在工作:) – shraddha 2010-10-30 21:37:17

0

你的代碼看起來不錯,在我的結束它工作正常,所以我不知道這是正確的或錯誤的答案。

,但如果沒有,那麼工作,你可以嘗試爲你的第一個問題

[HttpGet] 
    public ActionResult Create() 
    { 
     return View("Configuration"); 
    } 

    [HttpPost] 
    public ActionResult Create(FormCollection collection) 
    { 


    //your code 
    } 

和爲貴(返回列表)鏈接,你需要

[HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
在你看來

和一多,您必須啓用客戶端沉默驗證腳本

<%Html.EnableClientValidation(); %>

和你的第二答案

//after your action executed 
    protected override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     //your code 
     base.OnActionExecuted(filterContext); 
    } 



    //before your action execute 
    protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     //your code 
     base.OnActionExecuting(filterContext); 
    } 
相關問題