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

Multiple log with different settings with log4net and autofac
How to Allow White space between words in MVC Regular Expression
Ad Pop Up when my website loads
Query String Parameter used as View Name
Azure AAD Role Based Authentication, User.IsInRole()
how to collapse all grouped row in pageble kendo grid?
Scheduled Job equivalent functionality in MVC
Website unavailable after scaling down on Azure
ASP.NET MVC antiforgery with 2 forms inside view
How can send html text box value with Url.Action to controller action method input argument?
Different between api scope and user claim in identityserver4
IgnoreRoute with file extension is not working in MVC
How to display duplicate values and unique values in two different tables Entity Framework
Kendo Grid MVC Check Box disable attribute is updating by default
Button repeatedly calls GET method and does not POST when editing
Do i need a new controller for this?

Categories

HOME
sbt
regex
ssis
apache-spark
demandware
identityserver4
orbeon
auth0
apt-get
compiler-errors
haskell-stack
python-3.5
twitter-digits
wikipedia
pyusb
classloader
asp.net-mvc-3
bluej
wavelet
command-line-arguments
export-to-excel
smartsheet-c#-sdk-v2
aar
zap
silk-performer
gitlab-ci-runner
dsc
abstract-syntax-tree
simple-html-dom
android-button
android-arrayadapter
android-imageview
file-manager
clappr
bluebird
mobile-center
pyparsing
groove
advanced-installer
outlook-vba
qweb
apartment-gem
runc
ibm-wcm
jpype
mrtg
koala-framework
canopen
master-data-management
sshd
manual
untagged
jcuda
android-sdk-tools
tfs2008
perspective
facebook-audience-network
tealeaf
xpdf
visual-build-professional
formatter
geneos
vsvim
groupbox
xelement
membership
materialdrawer
oboe.js
sketching
simperium
size-t
fortrabbit
applicationstate
nolio
httpservice
android-menu
lwuit-list
first-class-functions
ash
eventmachine
asp.net-apicontroller
delphi-2009
android-c2dm
qt4.7
antisamy
disabled-input
nmea
modx-evolution
simpleaudioengine
flash-cs4
kmz
chromeless
wpf-4.0
guvnor
xml-dtd
android-assets
jammit
paredit
jdic
maemo
polyglot
msn-messenger
project-settings
shareware
corporate-policy

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