0

I need to pass 2 parameters id and date from view to controller side on the click event. It may be basic question but i am not able to do so.

I tried simply this code

Code <a href='/Abc/Details/id?=@website_id, date?=@date' class="" id="prev" >Prev</a>

and how to get those parameter at controller side.

I don't want to use "Ajax" or JavaScript if possible

5
  • 2
    .../Details?id=@website_id&date=@date" but why are you not using @Html.ActionLink() to generate this correctly? - @Html.ActionLink("Prev", "Details", "Abc", new { id = "@website_id", date = "@date" }, new { id = "prev" }) Commented Feb 8, 2016 at 5:59
  • @StephenMuecke:I tried that but It dosen't work for me Commented Feb 8, 2016 at 6:51
  • Then you have made a mistake and not used the code I showed, or you have other errors in your code. Commented Feb 8, 2016 at 6:53
  • I don't receive any parameters at controller side by your suggestion. yes may be you are right but it didn't work for me that's what i am said Commented Feb 8, 2016 at 7:11
  • Then your not passing any values in the parameters - just hard code it as @Html.ActionLink("Prev", "Details", "Abc", new { id = "1", date = "2016-2-8" }, new { id = "prev" }) to see how it works Commented Feb 8, 2016 at 7:14

3 Answers 3

4

First of all either you need to create custom route or enable MapMvcAttributeRoutes in your routeconfig file by adding below line of code.

routes.MapMvcAttributeRoutes();

Then in your controller above your defined action add something like below.

[Route("/Abc/Details/{id}/{date}")]

If you want to make it nullable then.

[Route("/Abc/Details/{id?}/{date?}")]

Your action method will be something like below.

[Route("/Abc/Details/{id?}/{date?}")]
public ActionResult Details(int id, string date)

Use @Html.ActionLink instead of hard coding your links.

If you wanted to go with custom route then add it above your default route.

routes.MapRoute(
                "MyCustomRoute",
                "Archive/{entrydate}",
                new { Controller = "ABC", action = "Details",Id  = UrlParameter.Optional,Date =  UrlParameter.Optional});

Now in your view

@Html.RouteLink("Link Text", "MyCustomRoute", new { Id = YourId, Date=YourDate})
Sign up to request clarification or add additional context in comments.

Comments

1

I found it easier to declare your variables in the Controller Action:

public IActionResult Create(string FirstName, string MiddleName, string    
LastName, string ADUsername, string ADId, string ADName, string ADemail)
{
    //do this
}

Then you can assign the variables by name in the View:

<a asp-controller="Employees" asp-action="Create" asp-route-   
FirstName="@item.FirstName" asp-route-MiddleName="@item.MiddleName" asp-
route-LastName="@item.LastName" asp-route-ADUsername="@item.ADUsername" asp-
route-ADId="@item.ADId" asp-route-ADName="@item.ADName" asp-route-
ADemail="@item.ADemail">Add Employee</a>

Comments

1

I found @Mighty Ferengi answer very useful. Thanks man!

public IActionResult Create(string FirstName, string MiddleName, string    
LastName, string ADUsername, string ADId, string ADName, string ADemail)
{
//do this
}

In Razor

<a asp-controller="Employees" asp-action="Create" asp-route-   
FirstName="@item.FirstName" asp-route-MiddleName="@item.MiddleName" asp- route-LastName="@item.LastName" 
asp-route ADUsername="@item.ADUsername" 
asp-route-ADId="@item.ADId" asp-route ADName="@item.ADName" 
asp-route- ADemail="@item.ADemail">Add Employee</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.