asp.net-mvc


MVC - set multiple ActionLink and TextBox properties inside the controller


I want to be able to set multiple form(html) objects (Textbox, ActionLink) to either visible = false or enabled = false based on a condition inside the controller.
In web forms I would do something like the code below. What is the best way to handle this in MVC inside the controller? Thanks!
switch (UserSession.AppUserAccessLevel)
{
case AccessLevel.FullAdmin:
txtLastName.Enabled = true;
lnkExportData.Visible = true;
btnSubmit.Enabled = true;
break;
case AccessLevel.Admin:
txtLastName.Enabled = true;
lnkExportData.Visible = false;
btnSubmit.Enabled = false;
break;
case AccessLevel.ReadOnly:
lnkExportData.Visible = false;
btnSubmit.Enabled = false;
break;
}
For doing this in MVC you need to user Model and pass the Model to View through controller. Please find the example code below. You can loop through text boxes and set the property using Razor view.
Controller Action:
public ActionResult YourAction()
{
//Sets your property
string yourProperty = _db.GetProperties().FirstOrDefault();
//Passing in "yourProperty" as the Model to the View
return View(yourProperty);
}
//Alternativately you can bind the property to a Model that has multiple properties
public ActionResult YourAction()
{
YourModelClass model = new Model();
model.Property = _db.GetProperties().FirstOrDefault();
return View(model);
}
Within your View (Top of your View): (The name should match the name of the Controller Action - so YourAction.cshtml / YourAction.aspx)
//You need to firstly bind your Model to a type.
#Model string //This is your first example above. It simply binds the Model to a string
#Model YourModelClass //This binds the Model (and all of its properties) to the passed in YourModelClass
Populating your Textboxes (Again - within your View)
//Now you can access and populate a textbox in the methods previously listed
#Html.TextBox("TextBoxName",Model) //If using a string for the Model
#Html.TextBox("TextBoxName",Model.YourProperty) //If using YourModelClass as the Model
or
#Html.TextBoxFor(model => model.YourProperty) //Again - using YourModelClass

Related Links

consuming select list from different class mvc
Pass MVC / Razor object to javascript / knockout?
Compiling razor files slow when many references
MVC PartialView()
MVC - How to ModelBind a list an objects with this unusual post data format?
How to populate a ViewModel object after joining two entities together
Insert values to entity framework in asp.net mvc4
sending two models to partial view
Need to upgrade a simple page post back form to use some row “inpage” postback mechanism?
Miniprofiler apperas in IE 9 but not in Chrome or Firefox
BEM and asp.net mvc [closed]
Using dataannotations displayFormat to change to title case in ASP.Net MVC
Websecurity.CreateUserAndAccount on Azure doesn't work
Can I use Ninject to inject dependencies into attributes rather than using the service locator pattern?
How to handle a null reference exception inside html helpers
MVC ASPX To Razor - Register webcontrol

Categories

HOME
dicom
appium-ios
streaming
robot
go-gorm
oracle-adf
mvvm-light
ibm-midrange
sony
add-in
mobilefirst-adapters
hyperion
oracle-ucm
development-environment
python-2.5
spring-cloud-consul
image-gallery
amplitude
pydev
opencms
powershell-v2.0
ssms-2014
abstract-syntax-tree
mathdotnet
debugview
complex-networks
gosublime
lifecycle
capstone
google-crawlers
word-embedding
gradle-tooling-api
permgen
mediawiki-api
nscalendar
log4perl
complement
view-source
curly-braces
masspay
geneticsharp
cabal-install
android-sdk-tools
sscanf
vistadb
surroundscm
measurement-protocol
xceed
building
sizeof
enhanced-ecommerce
xlform
mysqladmin
asp.net-authorization
linqtocsv
uac
ssha
materialdrawer
symbian
gulp-protractor
cvc4
structured-programming
xerces
omap
wintersmith
wso2as
newsql
horizontalscrollview
jasmin
typesafe-activator
razor-2
concurrentdictionary
nhunspell
magic-unipaas
jxta
preon
libtorrent-rasterbar
semantic-markup
floating-point-conversion
repeating
jmdns
xsocket
cocosbuilder
ad-hoc-distribution
adaboost
httpconnection
semantic-zoom
stumbleupon
openid4java
process.start
pong
staging
mkmapviewdelegate
xoom
executescalar
webshop
xquery-update
resharper-4.5
versant-vod
ants

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