Click here to Skip to main content
15,886,873 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: how to do a a parody album management ASP.NET application Pin
OriginalGriff2-Nov-15 21:32
mveOriginalGriff2-Nov-15 21:32 
GeneralRe: how to do a a parody album management ASP.NET application Pin
kushaochin2-Nov-15 21:43
kushaochin2-Nov-15 21:43 
GeneralRe: how to do a a parody album management ASP.NET application Pin
OriginalGriff2-Nov-15 21:49
mveOriginalGriff2-Nov-15 21:49 
GeneralRe: how to do a a parody album management ASP.NET application Pin
Richard MacCutchan2-Nov-15 21:53
mveRichard MacCutchan2-Nov-15 21:53 
GeneralRe: how to do a a parody album management ASP.NET application Pin
kushaochin2-Nov-15 21:56
kushaochin2-Nov-15 21:56 
GeneralRe: how to do a a parody album management ASP.NET application Pin
OriginalGriff2-Nov-15 22:03
mveOriginalGriff2-Nov-15 22:03 
QuestionMVC, populating a dropdownlist, values persist on postback, and setting the value. Pin
jkirkerx2-Nov-15 12:16
professionaljkirkerx2-Nov-15 12:16 
AnswerRe: MVC, populating a dropdownlist, values persist on postback, and setting the value. [Sort of Solved] Pin
jkirkerx4-Nov-15 7:26
professionaljkirkerx4-Nov-15 7:26 
Well it works, but I'm sure over time I will figure out a more efficient way to do this.
I did complete this 2 hours after the first posts time stamp.

In the Model, I change the Security Level to a string, and just casted it from int to string and back in Linq for my Entity Framework.
[Required]
[Display(Name = "Security Level")]
public string SecurityLevel { get; set; }

public Dictionary<string, string> AdminSecurityLevels { get; set; }

[Edit] Persist across postbacks
In the Controller to make the values persist across postbacks, I found you do have to call the class function twice, once for ActionResult and once for ActionResult Post. So sa.AdminSecurityLevels is my Dictionary of Key Values.
// Load the DropDownList and RadioButtonList
sa.AdminSecurityLevels = SelectListHelper.GetAdminSecurityLevels();

In my Class, in App_Code, I made a helper that is called in the controller above.
I wanted to use IEnumerator, but could not figure out how to get it to work, so I went with Dictionary. I guess since my security levels are integers, and the HTML5 uses all strings in the option element, I just went with <<string string>>
public class SelectListHelper
    {
        public static Dictionary<string, string> GetAdminSecurityLevels()
        {
            return new Dictionary<string, string>
            {
                {"5 - Super Administrator", "5"},
                {"4 - Management", "4"},
                {"3 - Website Operator", "3"},
                {"2 - Field Employee", "2"},
                {"1 - Demonstration", "1"}
            };
        }
    }
Finally the View, this part was hard to understand using Razor. I don't quite understand the use of m => m.SecurityLevel versus Model.AdminSecurityLevels or m versus Model.
The new SelectList converts the Dictionary to a Key Value Pair that Razor can use to create option elements out of the collection.

[Edit]
Now the weird part, is that I had to reverse the Key Value to Value Key. That part I don't understand.
<div class="form-group">
 @Html.LabelFor(m => m.AdminSecurityLevels, new { @class = "control-label" })
 @Html.DropDownListFor(m => m.SecurityLevel,
    new SelectList(Model.AdminSecurityLevels, "Value", "Key"), new { @class = "form-control" })
</div>

Final Thoughts on this, wow that was a lot of work just to populate a dropdownlist or radiobutton list. I suppose in my future, as I write more code for these list, I will build a collection of them such as states and countries, and I can just add to the model, call it in the controller and just add it to my razor statement in less than 60 seconds.

Getting the whole thing to work including the jquery.unobtrusive.validator, Bootstrap.Css, MVC in general as a first time for me was a pain in the ass. But flashing back to Webforms and using new Listitem did take me a while to figure out as well. Oh well, new technology, hopefully it pays off in the future for me.
QuestionLosing the content of file upload control on postback Pin
Member 114228682-Nov-15 1:09
Member 114228682-Nov-15 1:09 
AnswerRe: Losing the content of file upload control on postback Pin
Richard Deeming2-Nov-15 2:19
mveRichard Deeming2-Nov-15 2:19 
AnswerRe: Losing the content of file upload control on postback Pin
F-ES Sitecore9-Nov-15 5:08
professionalF-ES Sitecore9-Nov-15 5:08 
QuestionFloor Plan in Web Application in ASP .NET Pin
Member 77075161-Nov-15 21:42
Member 77075161-Nov-15 21:42 
AnswerRe: Floor Plan in Web Application in ASP .NET Pin
F-ES Sitecore2-Nov-15 0:28
professionalF-ES Sitecore2-Nov-15 0:28 
QuestionCheckbox with enable and disable function. Pin
Praveen Kandari30-Oct-15 20:07
Praveen Kandari30-Oct-15 20:07 
QuestionGetting error when I try to load report in ASP.Net application Pin
Member 1209746129-Oct-15 2:15
Member 1209746129-Oct-15 2:15 
Questionsession related issues. Pin
Member 1191972228-Oct-15 19:16
Member 1191972228-Oct-15 19:16 
AnswerRe: session related issues. Pin
F-ES Sitecore28-Oct-15 23:18
professionalF-ES Sitecore28-Oct-15 23:18 
GeneralRe: session related issues. Pin
Member 1191972228-Oct-15 23:42
Member 1191972228-Oct-15 23:42 
GeneralRe: session related issues. Pin
F-ES Sitecore28-Oct-15 23:58
professionalF-ES Sitecore28-Oct-15 23:58 
GeneralRe: session related issues. Pin
Member 1191972229-Oct-15 0:04
Member 1191972229-Oct-15 0:04 
GeneralRe: session related issues. Pin
F-ES Sitecore29-Oct-15 0:13
professionalF-ES Sitecore29-Oct-15 0:13 
GeneralRe: session related issues. Pin
Member 1191972229-Oct-15 19:38
Member 1191972229-Oct-15 19:38 
GeneralRe: session related issues. Pin
F-ES Sitecore29-Oct-15 22:23
professionalF-ES Sitecore29-Oct-15 22:23 
GeneralRe: session related issues. Pin
Member 1191972229-Oct-15 23:29
Member 1191972229-Oct-15 23:29 
GeneralRe: session related issues. Pin
F-ES Sitecore29-Oct-15 23:36
professionalF-ES Sitecore29-Oct-15 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.