c# - Unable to call Post ActionResult from View -
i trying make post request view calling actionresult in controller. there list of events in view , user can view details of event clicking event. part works. however, once view details have ability sign event. part not working.
a sample action i'm trying view:
@html.actionlink("signup", "signup", new {id = "2"}, null)
this should access action result:
[httppost] [validateantiforgerytoken] public actionresult signup(int id) { if (modelstate.isvalid) { var registratie = new registratie(user.identity.getuserid(), id); _db.registraties.add(registratie); _db.savechanges(); return redirecttoaction("index"); } return view("index"); }
however, getting 404 error. think can't find actionresult?
the details action result on same page , works:
// get: /eventplanner/details/5 public actionresult details(int id) { var evenement = _db.evenementen.single(e => e.id == id); return view(evenement); }
i don't understand why signup gives 404. ideas?
you cant use actionlink
making post
request. have following alternatives.
- use
submit
button post form - use
ajax.actionlink()
- use
jquery.ajax
.
i recommend submit
button because feel simpler rest.
as example first approach. try this
razor:
@using (@html.beginform("actionname", "controllername")) { <input type="hidden" name="id" value="2" /> <input type="submit" value="post" /> }
controller:
public class controllernamecontroller : controller { [httppost] public actionresult actionname(string id) { //your stuff return view(); } }
Comments
Post a Comment