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?