Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing integer values from view to controller (mvc) through href

Tags:

asp.net-mvc

I 'm doing a dot net project (MVC) in which I want to pass integers (hard coded university ids) from view to Controller. In view I 'm doing

<a href="/Admin/applied/1">PU</a>
<a href="/Admin/applied/2">UET</a>
<a href="/Admin/applied/3">GC</a>

where admin is the CONTROLLER & applied is ACTION METHOD

in ACTION METHOD (applied in Admin)

public ActionResult applied(string uniId)
{
    Processing of admissions on the basis of uniId
}

but when I 'm compiling this , uniId contains NULL instead of actual integer passed by <a href="/Admin/applied/1">PU</a> etc. Kindly help me out

like image 276
Muhammad Faisal Iqbal Avatar asked Jul 11 '26 15:07

Muhammad Faisal Iqbal


1 Answers

ASP.NET MVC will only provide the value if the parameter is named id. You have to options. First is renaming the parameter:

public ActionResult applied(string id)
{
    Processing of admissions on the basis of uniId
}

Second is creating a new route in RouteConfig above the default route:

routes.MapRoute(
        "Applied", // Route name
        "Admin/applied/{uniId}", // URL with parameters
        new { controller = "Admin", action = "applied" } // Parameter defaults
        );
like image 190
Ufuk Hacıoğulları Avatar answered Jul 14 '26 13:07

Ufuk Hacıoğulları



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!