Reputation: 77
I'm quite new to MVC styled pages, but slowly learning. I want to be able to click an actionlink which will then pass a parameter to my controller which will use it to get something from the database, but I've tried a number of different things and the controller is only ever given null as the parameter.
In my view, I've constructed my action link as follows:
@Html.ActionLink(Model.Name, "GetMeeting", "ChooseMeeting", null, new {meetingid = Model.MeetingId})
And in my Controller that deals with this request is the following:
public ActionResult GetMeeting(string meetingid)
{
var races = RaceRepository.GetRacesInMeeting(new Meeting() {MeetingId = new Guid(meetingid)});
return View(races);
}
The meetingID is always null, can someone please help me with this and let me know how to give it the parameter I want. Also, I've checked and in the view, Model.MeetingID is populated with the ID it should be.
Any help would be appreciated, Thanks Ryan
Upvotes: 1
Views: 99
Reputation: 77
doh! I worked it out, the problem was, I was calling the wrong overload for ActionLink. It should have been:
@Html.ActionLink(Model.Name, "GetMeeting", "ChooseMeeting",new {meetingid = Model.MeetingId},null)
Sorry for the time waste.
Cheers, Ryan
Upvotes: 0
Reputation: 9548
You very likely have not defined a route that passes the meetingId as a parameter that is recognized.
You intend that your ActionLink will resolve to the following (with 3 being the value of meetingId).
/ChooseMeeting/GetMeeting/3
What is actually being routed is probably this:
/ChooseMeeting/GetMeeting
This is due to the single, default MVC route being defined something very similar to this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Request", action = "Index", id = UrlParameter.Optional }
);
The problem here is the parameter name being "id" by default where you have named your parameter meetingId.
There are at least 3 ways to solve this:
id
so that the existing route will map the parameter as desired.meetingId
./ChooseMeeting/GetMeeting/?meetingId=3
Upvotes: 0
Reputation: 13402
Just out of the bat, I think your null might be in the wrong place. Assuming Model.name is just text, "GetMeeting" is an action name and "ChooseMeeting" is a controller name try something like this
@Html.ActionLink(Model.Name, "GetMeeting", "ChooseMeeting", new {meetingid = Model.MeetingId}, null)
And your action would be something like
public class ChooseMeeting : Controller
{
public ActionResult GetMeeting(int meetingid)
{
// ...
return View();
}
}
Upvotes: 2