-2

I have spent years mostly working in classic asp and am finally trying to come into the .net world. I have multiple items I need assistance with.

1) The create view has a drop down in it. Right now it is populated with all records in the table but only some of them should be shown. I have a stored procedure that is normally used to populate the drop down list, but do not know how to change it in the code to use the procedure. The class for the procedure shows

namespace AMS_MVC.Models
{
    using System;

    public partial class usp_ListRoles_Result
    {
        public byte RoleID { get; set; }
        public string RoleName { get; set; }
    }
}

here is what the controller has right now (generated by visual studio) using entity

ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName");

2) I do not want the first item in the list to be a default, I prefer to have the first item say "Make Selection" and not have a value. Therefore the administrative user cannot make a mistake and assign a person to the wrong role by forgetting to make a selection.

3) I need to add an onChange event to the drop down. Depending on the role selected, other options may be shown. For example if the role of department manager is selected then it should display a drop down list of departments, If the role of district manager is selected then it would display a list of districts etc.. In one option Location Department Manager it should display both a list of departments and also a list of locations.

In classic asp the onchange event calls a javascript function which i wrote that displays the correct hidden div(s).

Please people, I have been looking at many different example for a week now on this site as well as on others provided by google and bing searches and they are no help. if all you want to do is point to someone else's question that answers something similar but not exactly what I am looking for then please don't provide a link because it does not help me.

  • 1
    Read some "how to populate dropdowlist" mvc tutorials with HtmlHelper (here an example http://stackoverflow.com/questions/20567364/asp-net-mvc-populate-dropdownlist), for point 3 you can do the same as "classic asp". – Logar314159 Jan 13 '16 at 18:23
  • That link provided absolutely NO help to me. – Miranda Johnson Jan 13 '16 at 18:48
  • For the item 1 depends on how you're connected to your data source. For the item 2, check http://stackoverflow.com/questions/16073464/asp-net-mvc-how-to-add-placeholder-for-html-dropdownlist. For the item 3 you can follow http://stackoverflow.com/questions/25056508/onchange-event-for-html-dropdownlist or http://stackoverflow.com/questions/8973037/handling-onchange-event-in-html-dropdownlist-razor-mvc – juliano.net Jan 13 '16 at 18:49
  • using entity framework – Miranda Johnson Jan 13 '16 at 19:19
  • @MirandaJohnson - You're getting upset because you want ASP.NET MVC to work like your old way of doing things. It doesn't work that way. You will need to learn more about the way ASP.NET MVC works, and that is what people are trying to point you to. Just giving you an example won't work for you because there won't be an example of EXACTLY what you need, you will have to understand it enough to adapt a sample. – Erik Funkenbusch Jan 13 '16 at 19:58
  • @Erik I am frustrated because I have been searching on Google and Bing for a couple weeks and seen hundreds of pages point to the same items, no matter how I format my search. If they helped I would not need to ask for help on here. – Miranda Johnson Jan 13 '16 at 20:46

2 Answers2

1

In MVC there is a DropDownListFor method from HtmlHelper class. Just use it with your model property (which must be of type List<T>)

@Html.DropDownListFor(m => m.Field, Model.List, new { @id = "id", @class = "css-class" }), where m.Field is a property in your model where to store selected value, Model.List is a list of displayed items. It is also possible to create this using a "classic asp"

Andrew
  • 1,474
  • 4
  • 19
  • 27
1

Thank you. that answers the 3rd part of my question about adding the onchange event to the list.

original code 
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control" }) 

new code    
@Html.DropDownList("RoleID", null, htmlAttributes: new { @class = "form-control", @onchange="displayOptions(this.value)"})  

For part 2 how to add another option at the beginning of the list i am stumped

What dropdown shows in MVC

What I need the drop down to show for first option

also by classic asp i did not mean asp.net i meant asp 3.0 and it is TOTALLY different than asp.net.

  • 1
    You use the parameter called "option string" in the MVC Helper object to specify the default (unselected) state. For instance `@Html.DropDownListFor(m => m.Field, Model.List, "Make a selection", new { @id = "id", @class = "css-class" })` – Erik Funkenbusch Jan 13 '16 at 21:36