I'm learning ASP.NET MVC and I have the following problem.
The view "SelectProdotti" is
@model Models.SelectProdottiModel
@{
ViewBag.Title = "Select Prodotti";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("ProdottiToListino", "Listino", FormMethod.Post))
{
@Html.HiddenFor(m => m.id)
@Html.DisplayFor(m=> m.id)
<input type="submit" value="Salva" />
}
The action that load the view
public ActionResult SelectProdotti(int id)
{
SelectProdottiModel model = new SelectProdottiModel();
model.id = id;
return View(model);
}
The model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Models
{
public class SelectProdottiModel
{
public int id;
public SelectProdottiModel()
{
}
}
}
The post action in the controller
[HttpPost]
public ActionResult ProdottiToListino(SelectProdottiModel model)
{
return RedirectToAction("SelectProdotti", "Listino", new {id = model.id });
}
I have written this code only to learn, it is useless. The problem is that model.id is always 0, i.e. the view don't post the value, where is the error ?
public int idis a field. The default model-binder won't even try to bind it. Try with a property instead:public int id { get; set; }