MultiselectList with selected values are not pre selected in ViewHow do you give a C# Auto-Property a default value?A potentially dangerous Request.Form value was detected from the clientCompile Views in ASP.NET MVCGet int value from enum in C#How to loop through all enum values in C#?How do I import a namespace in Razor View Page?MVC Update FormsAuthenticationTicket UserData at RuntimeSet value to checkbox & retrieve that value in controllercombine multiple rows/ columns into single row SQL ASP.NETPassing parameters on button click in strongly-typed view to another controller
How can I draw a rectangle around venn Diagrams?
How can I test a shell script in a "safe environment" to avoid harm to my computer?
Explaining intravenous drug abuse to a small child
Function annotation with two or more return parameters
While drilling into kitchen wall, hit a wire - any advice?
call() a function within its own context
Test whether a string is in a list with variable
Searching for a sentence that I only know part of it using Google's operators
Why doesn't increasing the temperature of something like wood or paper set them on fire?
How is it believable that Euron could so easily pull off this ambush?
Good introductory book to type theory?
Where do 5 or more U.S. counties meet in a single point?
Why was Gemini VIII terminated after recovering from the OAMS thruster failure?
What is more safe for browsing the web: PC or smartphone?
Assuming a normal distribution: what is the sd for a given mean?
How does "politician" work as a job/career?
Translation of "invincible independence"
A♭ major 9th chord in Bach is unexpectedly dissonant/jazzy
What is the meaning of "matter" in physics?
I want to write a blog post building upon someone else's paper, how can I properly cite/credit them?
Why always 4...dxc6 and not 4...bxc6 in the Ruy Lopez Exchange?
Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?
Does restarting the SQL Services (on the machine) clear the server cache (for things like query plans and statistics)?
Did Ham the Chimp follow commands, or did he just randomly push levers?
MultiselectList with selected values are not pre selected in View
How do you give a C# Auto-Property a default value?A potentially dangerous Request.Form value was detected from the clientCompile Views in ASP.NET MVCGet int value from enum in C#How to loop through all enum values in C#?How do I import a namespace in Razor View Page?MVC Update FormsAuthenticationTicket UserData at RuntimeSet value to checkbox & retrieve that value in controllercombine multiple rows/ columns into single row SQL ASP.NETPassing parameters on button click in strongly-typed view to another controller
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a simple service which is :
1. Getting All roles in a system
2. Check the role of current selected user
3. Render all roles in a dropdown , selecting current user roles by default
To do that , I have following code:
public ActionResult Edit(string userId)
var user = _oAuthUserService.GetUsers()?.Where(u => u.UserId == userId).FirstOrDefault();
var userRoles = user.Roles; //[Admin,Manager]
var allRolesFromService = _oAuthUserService.AllRoles.Select(x=>new
Id =x.RoleName,
Name =x.RoleName
).ToList(); //All roles in the System [Role1,Role2,Role3,Admin,Manager]
ViewData["AllRoles"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
return View(user);
In view I am doing ,
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" );
But when I navigate to view , none of the items are pre-selected? What am I missing here?
c# asp.net asp.net-mvc asp.net-mvc-4 razor
add a comment |
I have a simple service which is :
1. Getting All roles in a system
2. Check the role of current selected user
3. Render all roles in a dropdown , selecting current user roles by default
To do that , I have following code:
public ActionResult Edit(string userId)
var user = _oAuthUserService.GetUsers()?.Where(u => u.UserId == userId).FirstOrDefault();
var userRoles = user.Roles; //[Admin,Manager]
var allRolesFromService = _oAuthUserService.AllRoles.Select(x=>new
Id =x.RoleName,
Name =x.RoleName
).ToList(); //All roles in the System [Role1,Role2,Role3,Admin,Manager]
ViewData["AllRoles"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
return View(user);
In view I am doing ,
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" );
But when I navigate to view , none of the items are pre-selected? What am I missing here?
c# asp.net asp.net-mvc asp.net-mvc-4 razor
1
Try replacing yourViewData
key name, becauseAllRoles
already used to bound withListBox
, avoiding potential naming collision. Alternatively useListBoxFor
with strongly-typed viewmodel property.
– Tetsuya Yamamoto
Mar 19 at 7:07
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12
add a comment |
I have a simple service which is :
1. Getting All roles in a system
2. Check the role of current selected user
3. Render all roles in a dropdown , selecting current user roles by default
To do that , I have following code:
public ActionResult Edit(string userId)
var user = _oAuthUserService.GetUsers()?.Where(u => u.UserId == userId).FirstOrDefault();
var userRoles = user.Roles; //[Admin,Manager]
var allRolesFromService = _oAuthUserService.AllRoles.Select(x=>new
Id =x.RoleName,
Name =x.RoleName
).ToList(); //All roles in the System [Role1,Role2,Role3,Admin,Manager]
ViewData["AllRoles"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
return View(user);
In view I am doing ,
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" );
But when I navigate to view , none of the items are pre-selected? What am I missing here?
c# asp.net asp.net-mvc asp.net-mvc-4 razor
I have a simple service which is :
1. Getting All roles in a system
2. Check the role of current selected user
3. Render all roles in a dropdown , selecting current user roles by default
To do that , I have following code:
public ActionResult Edit(string userId)
var user = _oAuthUserService.GetUsers()?.Where(u => u.UserId == userId).FirstOrDefault();
var userRoles = user.Roles; //[Admin,Manager]
var allRolesFromService = _oAuthUserService.AllRoles.Select(x=>new
Id =x.RoleName,
Name =x.RoleName
).ToList(); //All roles in the System [Role1,Role2,Role3,Admin,Manager]
ViewData["AllRoles"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
return View(user);
In view I am doing ,
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" );
But when I navigate to view , none of the items are pre-selected? What am I missing here?
c# asp.net asp.net-mvc asp.net-mvc-4 razor
c# asp.net asp.net-mvc asp.net-mvc-4 razor
edited Mar 23 at 6:01
ArunPratap
1,99631128
1,99631128
asked Mar 19 at 7:01
SimsonsSimsons
5,22730107210
5,22730107210
1
Try replacing yourViewData
key name, becauseAllRoles
already used to bound withListBox
, avoiding potential naming collision. Alternatively useListBoxFor
with strongly-typed viewmodel property.
– Tetsuya Yamamoto
Mar 19 at 7:07
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12
add a comment |
1
Try replacing yourViewData
key name, becauseAllRoles
already used to bound withListBox
, avoiding potential naming collision. Alternatively useListBoxFor
with strongly-typed viewmodel property.
– Tetsuya Yamamoto
Mar 19 at 7:07
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12
1
1
Try replacing your
ViewData
key name, because AllRoles
already used to bound with ListBox
, avoiding potential naming collision. Alternatively use ListBoxFor
with strongly-typed viewmodel property.– Tetsuya Yamamoto
Mar 19 at 7:07
Try replacing your
ViewData
key name, because AllRoles
already used to bound with ListBox
, avoiding potential naming collision. Alternatively use ListBoxFor
with strongly-typed viewmodel property.– Tetsuya Yamamoto
Mar 19 at 7:07
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12
add a comment |
1 Answer
1
active
oldest
votes
The problem occurs because you're using same ViewData
key name as model name bound to ListBox
helper in this line:
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" )
Assumed that AllRoles
is an array or list property inside viewmodel class, you can't reuse it as ViewData
key name because naming collision will happen between them, causing the option lists won't show up properly. You can rename ViewData
key name with any different name than viewmodel's property name:
ViewData["AllRolesList"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
And then use new key name within ListBox(For)
helper:
ListBox helper
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
ListBoxFor helper
@Html.ListBoxFor(model => model.AllRoles, (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55235261%2fmultiselectlist-with-selected-values-are-not-pre-selected-in-view%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem occurs because you're using same ViewData
key name as model name bound to ListBox
helper in this line:
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" )
Assumed that AllRoles
is an array or list property inside viewmodel class, you can't reuse it as ViewData
key name because naming collision will happen between them, causing the option lists won't show up properly. You can rename ViewData
key name with any different name than viewmodel's property name:
ViewData["AllRolesList"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
And then use new key name within ListBox(For)
helper:
ListBox helper
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
ListBoxFor helper
@Html.ListBoxFor(model => model.AllRoles, (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
add a comment |
The problem occurs because you're using same ViewData
key name as model name bound to ListBox
helper in this line:
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" )
Assumed that AllRoles
is an array or list property inside viewmodel class, you can't reuse it as ViewData
key name because naming collision will happen between them, causing the option lists won't show up properly. You can rename ViewData
key name with any different name than viewmodel's property name:
ViewData["AllRolesList"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
And then use new key name within ListBox(For)
helper:
ListBox helper
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
ListBoxFor helper
@Html.ListBoxFor(model => model.AllRoles, (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
add a comment |
The problem occurs because you're using same ViewData
key name as model name bound to ListBox
helper in this line:
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" )
Assumed that AllRoles
is an array or list property inside viewmodel class, you can't reuse it as ViewData
key name because naming collision will happen between them, causing the option lists won't show up properly. You can rename ViewData
key name with any different name than viewmodel's property name:
ViewData["AllRolesList"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
And then use new key name within ListBox(For)
helper:
ListBox helper
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
ListBoxFor helper
@Html.ListBoxFor(model => model.AllRoles, (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
The problem occurs because you're using same ViewData
key name as model name bound to ListBox
helper in this line:
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRoles"], new multiple = "multiple" )
Assumed that AllRoles
is an array or list property inside viewmodel class, you can't reuse it as ViewData
key name because naming collision will happen between them, causing the option lists won't show up properly. You can rename ViewData
key name with any different name than viewmodel's property name:
ViewData["AllRolesList"] = new MultiSelectList(allRolesFromService, "Id", "Name", userRoles);
And then use new key name within ListBox(For)
helper:
ListBox helper
@Html.ListBox("AllRoles", (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
ListBoxFor helper
@Html.ListBoxFor(model => model.AllRoles, (MultiSelectList)ViewData["AllRolesList"], new multiple = "multiple" )
answered Mar 19 at 7:18
Tetsuya YamamotoTetsuya Yamamoto
17.2k42342
17.2k42342
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55235261%2fmultiselectlist-with-selected-values-are-not-pre-selected-in-view%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try replacing your
ViewData
key name, becauseAllRoles
already used to bound withListBox
, avoiding potential naming collision. Alternatively useListBoxFor
with strongly-typed viewmodel property.– Tetsuya Yamamoto
Mar 19 at 7:07
And changing viewDataKey Works!! Never though it could be the issue, Can you answer the question and I will accept as the solution.
– Simsons
Mar 19 at 7:12