0

I have a sql server table ProjectDetails. The table has following columns ID(PK), ProjectID, Project. I need the values of ProjectID & Project to be populated into the 2 different dropdown list.

Model:

public class ViewModel
    {
        public int ID { get; set; }
        public string Project { get; set; }
        public string ProjectID { get; set; }

    }

Controller:

 [ActionName("DetailsForm")]
    [HttpGet]
    public ActionResult DetailsForm()
    {
        try
        {
            IEnumerable<ViewModel> dropConfig = new List<ViewModel>();

            IEnumerable<ViewModel> selectList = floorService.DropDownList();

            ViewModel model = new ViewModel()
         {

             dropConfig = selectList,


         };

            return View("DetailsForm",model);

        }

I have difficulties in figuring out what should go under repository code where I need to execute the stored proc GetProjectDetails which will give me the data from Table ProjectDetails Please help me with some examples where I can learn to go ahead with my solution. Thanks in advance.

1
  • The code you have posted makes no sense. Your ViewModel class does not have a property named dropConfig but in your controller you have that code. Your code will not compile. Please post code which will compile and tell us which specific part you are having issues with. Commented Nov 14, 2017 at 4:23

1 Answer 1

2

In ASP.NET MVC, Dropdownlist is generated with SelectList Helper Class, So You need to use Select List Item and You can send the value with ViewBag

ViewBag.SelectList= new SelectList(_classObject.YourClassMethod(), "DropDownListValue", "DropDownListText");

In Razor View You Can Directly add it with Razor HTML Helper

@Html.DropDownList("SelectList", ViewBag.SelectList as SelectList,"Select Item", new {@class = "form-control" })

Other Hand If You Want to Use Strongly Typed

@Html.DropDownListFor(model => model.SelectList, ViewBag.SelectListas SelectList, "Select Item", new { @class = "form-control" })
Sign up to request clarification or add additional context in comments.

2 Comments

My question is how to get the values from database. There should be executereader or something right? I have a stored proc which has select statement.
You can use Entity Framework , Drapper or ADO.NET fetch these data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.