I have the following model:
public class Person
{
public int ID{get;set;}
public string Name {get;set;}
public string Address{get;set;}
}
For other hand i have the following view called Index:
@model List<Person>
@{
foreach(Person person in Model)
{
<a href="#" id="@person.ID">@person.Name</a>
}
}
Finally i have the following action:
public ActionResult Index()
{
List<Person> persons=new List<Person>();
persons.Add(new Person(){ID=1,Name="John"});
persons.Add(new Person(){ID=2,Name="Tom"});
persons.Add(new Person(){ID=2,Name="Derek"});
}
Im thinking to create a form (since i cannot use ajax for this app due to some requirements), to post an instance of the person chosen by the user (when clicks an anchor of my view). I would like to know how i could post a Person instance to another action described below (since my view is typed to a generic list of persons).
[HttpPost]
public ActionResult Index(Person person)
{
... Do whatever
}
Solution with POST:
in order to work it with
POSTyou will have to palce a form and plant hidden fields likeand on the server side
Solution with
GET:atags normally dont work with POST there default behaviour is to request the server for a resource viaGETalthough you can override this behaviour using javascript but in your case that is not an option therefore you can trythe only drawback is you will have to use a submit button instead of
atags, you can use css styling to style the button like anatag. also if you have not set custom routes for this kind of request the uri will have query string params like