asp.net-mvc


MVC pass int from view to controller with a button click


Here is my view:
#foreach (var item in Model)
{
if(i == 0)
{
#:<div class="row">
}
<div class="col-md-3 col-xs-12" style="margin-top:20px;">
#Html.ActionLink(item.Name, "ClientDetail", "Client", null, new { #class = "btn btn-primary btn-block", id = #item.id})
</div>
i++;
if(i%4 == 0)
{
#:</div>
i = 0;
}
}
and here is the controller:
public ActionResult ClientDetail(int id)
{
ClientModel model = cserv.Get(id, User.Identity.GetUserId());
ClientViewModel viewModel = new ClientViewModel(model);
return View(viewModel);
}
Every time I click on one of my links it gives me this error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ClientDetail(Int32)' in 'TechUCRM.Controllers.ClientController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Why is my id parameter not being passed into the controller?
Thank you!
You want to place id at routeValues argument.
#Html.ActionLink(item.Name, "ClientDetail", "Client",
new { id = item.id},
new { #class = "btn btn-primary btn-block"})
Same as
#Html.ActionLink(item.Name, "ClientDetail", "Client",
routeValues: new { id = item.id },
htmlAttributes: new { #class = "btn btn-primary btn-block" })
change this line:
#Html.ActionLink(item.Name, "ClientDetail", "Client", null, new { #class = "btn btn-primary btn-block", id = #item.id})
to:
#Html.ActionLink(item.Name, "ClientDetail", "Client", new{ id= item.Id }, new { #class = "btn btn-primary btn-block"})
one you used up there sets id attribute of HTML. it is not the Id for Routing.

Related Links

MVC Route with condition checking
ElmahR Dashboard throws “Unable to compile template” error when Sending Commands
VS2012 add areas to an existing project? start over? with MVC5 or stick with MVC4
Edit view from HttpPost method
Best practive for logging errors (handled and unhandled) in MVC
ServiceStack request giving 500 for large request
Cross domain session asp mvc in IE and old browsers
Adding MVC Controller scaffolding error
Unable to debug plugin in NopCommerce
Displaying HTML and Images in MultiSelectList ASP.NET MVC Razor
#Html.DisplayFor() value not change after the postback
Location of Validation Message
ASP.NET Identity update column type for primary key Id in AspNetUsers table from a string to and int or a guid, yes or no?
Deploying ASP MVC 5 App with IIS 7.5
validation for dropdown in MVC3
MVC getting a list of music and image into a table

Categories

HOME
validation
web-applications
identityserver4
copy
serial-port
watson
apacheds
passwords
google-cloud-dataproc
normalization
stripe-payments
mvvm-light
socrata
jscrollpane
deserialization
plots.jl
android-dialogfragment
records
vast
libpcap
game-maker-language
beanstalkd
dotnetbar
mouseevent
precision
italic
smallbasic
azure-redis-cache
toolbar
asana-api
jtapi
groove
webcenter
draggable
rights-management
jquery-ui-dialog
rtems
freecodecamp
ssms-2012
smartbanner
sql-mode
attributerouting
mspgcc
masm32
reactivex
javascript-security
itertools
db2400
sizing
cabal-install
topojson
file-import
tealeaf
xpdf
groups
webmail
mutation
xlform
jni4net
delphi-10-seattle
openejb
symbian
double-quotes
avalonedit
telescope
equinox
dhc
stringr
easynetq
emberfire
zim-database
janino
scribe
nolio
tab-delimited
jasmin
culture
razor-2
publish-actions
custom-titlebar
dirpagination
youtrack-api
dotliquid
validform
confirmation
nawk
vlfeat
siblings
emacs-jedi
polarssl
memoization
qdebug
mms-gateway
scrollpane
chromeless
speaker
src
java-ee-5
rc-shell
libavg
documentviewer
ccr
j#
molehill
f2c
bubble-popup
textboxlist
mkreversegeocoder
staging
webshop
cracker
non-relational-database
resharper-4.5
docking

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App