1

I got a intresting little problem with .NET MVC 4. I'm trying to learn how this works and after doing a few tutorials I keep running into the same problem.

Down here is my default Index method wich should be called when the page is loading.

   public ActionResult Index() {
       var result = (from Product in db.Products
                     orderby Product.Id ascending
                     select Product);
       return View(result.ToList());
   }

The method returns a list of product items from my database..

@using (Html.BeginForm("Index", "Stap1", FormMethod.Post, new { id = "form1" }))
{
    <select id="select" size="4" name="product">
        @foreach (var item in Model)
        {
            <option value="@item.Id" >@Html.DisplayFor(modelItem => item.Naam), @Html.DisplayFor(modelItem => item.Prijs)</option>            
        }
    </select>
    <input type="submit" value="Voeg Toe!" name="Add" />
}

I added a function to catch the post data of the form. like this..

[HttpPost]
public ActionResult Index(int product = 0) {
                var result = (from Product in db.Products
                              orderby Product.Id ascending
                              select Product);
                System.Diagnostics.Debug.WriteLine("wrong function called!");
                return View(result.ToList());
            }

As you can see it's pretty basic stuff, however, when I try to load the Index page. It calls the 'overloaded httppost Index()" instead of the normal one. Why does it call a HTTPPOST method when nothing is posted?

MichielDeRouter
  • 426
  • 4
  • 21
  • Post Action loads without Posting the form? – Akash KC Sep 27 '12 at 08:30
  • Or wrong method called when page loading. Any idea what might cause this? – MichielDeRouter Sep 27 '12 at 08:41
  • Unless you are specifically requiring the `id` of the form, you can just use the code `@using(Html.BeginForm)){ }` as this will automatically Post back to the same controller/action which you are on. e.g. /Stap1/Index will post back to /Stap1/Index by default. – Tim B James Sep 27 '12 at 08:53
  • I want it to load Index() on page load, then when I press the add button it calls the Index(int product) method. It's a basic Get page, Post data, Reload page view system but I cant get it working on the Index method. – MichielDeRouter Sep 27 '12 at 08:59
  • @user1069574: Agree with Tim B James. Your action results are fine and you can overload your actions the way you're doing it. As Tim said your FORM is posting to another action, though. Did you change anything in your RouteConfig? – LeftyX Sep 27 '12 at 09:16
  • Nope its a clean solution. I made it just to ask for this. And the code above is all that is to it. – MichielDeRouter Sep 27 '12 at 09:17
  • @user1069574: that's really weird. Can you put your code somewhere (ftp, dropbox, etc) so you can check it ? – LeftyX Sep 27 '12 at 09:22
  • Sure, but its made in visual studio 2012 ultimte on windows 8 (64 retail) i hope thats not a prob – MichielDeRouter Sep 27 '12 at 09:32
  • @user1069574: it shouldn't be. Let's see ... – LeftyX Sep 27 '12 at 09:40
  • There you go :) http://bositsolutions.nl/Wizard.rar It's a rar file containing the full project – MichielDeRouter Sep 27 '12 at 09:40

1 Answers1

2

I've checked your code and that is the right behavior. Your application starts with the HomeController which renders the Index view.

In your view I've found this piece of code:

@using (Html.BeginForm("Index", "Stap1", FormMethod.Post, new { id = "form1" }))
{
   <input type="submit" value="Start!" name="start" />
}

so you're actually posting to the Stap1Controller action Index.
If you want to call (HttpGet) your Index without posting you should do something like this:

@Html.ActionLink("Start!", "Index", "Stap1")
LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • You sir, are awesome. Thx a bunch, I never thought about it getting called from the previous page, seems like I have done some bad page connecting by making that form in the indexController. Thx a bunch! – MichielDeRouter Sep 27 '12 at 09:55