views:

67

answers:

3

Am trying my hands on asp.net mvc2 and using mvc2 template to work upon. Following the template, i created my own model, controller and view in there respective folder locations. i also changed the default routing in global.asax for this controller and view. now my view is getting loaded but when i click on a button in my view, none of the methods i wrote in my controller is getting hit. what could be the reason, what am i missing? i also want to call a preload method from my controller, before my view is rendered. please help...am stuck. here is my view

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

and here is my controller:

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

and here is my 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);
        }
    }
}

Please help, what am i missing?

A: 

This method should be getting called when you click the button, but all you are doing is going right back to the same View:

[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");
    }
}
Martin
Doing some break-point debug can help to confirm this.
xandy
+1  A: 

You need to specify the controller and action in when you invoke BeginForm

<% using(Html.BeginForm("Create", "Configuration")) {%>
pitx3
Thanks. It works now :)
shraddha
A: 

your code looks okay ,at my end it works fine so i'm not sure this is right or wrong answer.

but if it is not working then you could try for your first problem

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

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {


    //your code
    }

and for your ( Back to List ) link you need

    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

and one more in your view ,you have to enable Client sile Validation script

<% Html.EnableClientValidation(); %>

and your 2nd answer

    //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);
    }
Hasu