简体   繁体   中英

ASP.Net MVC - Pass parameter from view using a href

I currently have the following link that navigates from one view to another:

"<td class='EmployeeTableTD'><a href='personDetails'>" + item.Name + " " + item.Surname + "</a></td>" +

The table is dynamically created in Javascript and gets the data (item) from an ADO model.

I want to attach parameters to pass them through to the controller and then to the view of the next screen.

I have tried taking the usual

'personDetails?myParameter=' + item.Name

type of method, but this caused a 404 error. I also tried inserting a @HTML.ActionLink into the code, but this is also problematic as the intellisense does not recognise 'item' when trying to set the parameters.

Is there a way I can pass dynamic parameters from this view link to the next controller/view?

Controller:

using System;
...
namespace techTest4.Controllers
{
    public class HomeController : Controller
    {
        private TechTestEntities techContext = new TechTestEntities();

        public ActionResult personDetails()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

So my navigation correctly goes to 'Home/personDetails' and the 'personDetails.cshtml' view (which has little in it yet, but needs to output the parameters passed). It arguably should navigate to 'Home/personDetailsController/{someIndexMethod}' where the set up of the next page would be handled.

Config:

routes.MapRoute(
                name: "Details",
                url: "PeopleDetails/{action}/{id}",
                defaults: new
                {
                    controller = "PeopleDetails",
                    action = "All",
                    id = UrlParameter.Optional
                }
            ); 

You have to specify the parameters you're receiving in your action:

public ActionResult personDetails(String myParameter)
{
    return View();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM