0

first post! So I'm very new to asp.net mvc 4 (3 days) and I have been assigned to create a table to display some benchmark test results. I have three SQL data tables: Benchmark(has a foreign key on both the script and build IDs), Build (A table containing version, ID etc from a software), Script (names of some test scripts with date created, etc)

I have created the models. controllers and views for all the tables using the Entity Framework.

I would like for the user to select from a dropdown menu the build version, then to populate a table with all the scripts, the build version, and finally some benchmark components (average time, pass/fail, etc)

I have populated the dropdownlist with the data from the Build.Version.

Problem is I do not know how to retrieve the selected value from the DDL

Model class (Benchmark)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.WebPages.Html;

namespace BenchmarkApp.Models
{
   public partial class Benchmark
   {
       public int BenchmarkID { get; set; }
       public int ScriptID { get; set; }
       public int BuildID { get; set; }
       public int MinTime { get; set; }
       public int MeanTime { get; set; }
       public int MaxTime { get; set; }


       [ForeignKey("BuildID")]
       public virtual Build Build { get; set; }

       [ForeignKey("ScriptID")]
       public virtual Script Script { get; set; }
  }
}

Controller class for Benchmark specifically the ActionResult Index()

public class BenchmarkController : Controller
{
    private DAContext db = new DAContext();

    //
    // GET: /Benchmark/

    public ActionResult Index(string buildNumber = "0")
    {
        var benchmarks = db.Benchmarks.Include(b => b.Build).Include(b => b.Script);

        var query = db.Builds.Select(c => new { c.Id, c.Version });
        ViewBag.Builds = new SelectList(query.AsEnumerable(), "Id", "Version");

        return View(benchmarks.ToList());
    }

Finally the view (index) for the Benchmark, I have commented out some of the different DDL I have tried.

<table border="1">
    <tr>
        <th>
            Build version:
        </th>
        <th>
            <%-- Html.DropDownList("Builds", null, new {@onchange = "onChange(this.value);" }) --%>
            <%-- Html.DropDownList("BuildID", (SelectList) ViewBag.Builds, "--Select One--") --%>
            <%: Html.DropDownList("BuildDD", (IEnumerable<SelectListItem>)ViewBag.Builds, "--Select One--")%>
        </th>
    </tr>
</table>

Any help would be appreciated! Thanks in advance!

tereško
  • 58,060
  • 25
  • 98
  • 150
Stephen Sugumar
  • 545
  • 3
  • 9
  • 35
  • I believe this is what you need: [mvc c# html.dropdownlist and viewbag](http://stackoverflow.com/a/12112611/690329). – afzalulh Sep 14 '13 at 02:09

1 Answers1

0

try following code:

  @var listData = (List<SelectListItem>)ViewBag.Builds;
  @Html.DropDownList("Builds", listData, new
                                {
                                    onchange = "OnChange(this);",
                                    oval = listData.Any(x => x.Selected) ?
                                    listData.Find(x => x.Selected).Value : listData[0].Value
                                })

and in your javascript:

function ChangeLanguage_OnChange(dropdownControl) {
    var selectValue = dropdownControl.options[dropdownControl.selectedIndex].value;
}

Hope this will help you!

SoftSan
  • 2,482
  • 3
  • 23
  • 54