0
votes

Using Data Entity Framework, I created added my database. I have a table called Droptest, see models entry :

namespace DropMenu4FEB2018_FINAL.Models
{
    using System;
    using System.Collections.Generic;

    public partial class DropTest
    {
        public string DisplayList { get; set; }
        public string DisplayIndex { get; set; }
    }
}

CONTROLLER CODE :

Public action Create ()
{
    DROPMENUEntities db = new DROPMENUEntities();
    List<DropTest> list = db.DropTests.ToList();
    ViewBag.DropTestList = new SelectList(list, "DisplayIndex", "DisplayList");
    Return view ();
}

VIEW CODE :

@Html.LabelFor(model => model.Car, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.DropDownListFor(model => model.Car, ViewBag.DropTestList as SelectList, "--SELECT ONE--", new { @class = "form-control" @Html.ValidationMessageFor(model => model.Car, "", new { @class = "text-danger" })

The above controller and view code is working.

Now, I decided it would be more flexible if I used a stored procedure to return the contents of the table (to support filtered output in the future). I created the stored procedure (basic at this stage), which I called DisplayCars in SSMS, and it was named by data entity framework as DisplayCars_Result In the models you can view the stored procedure entry :

namespace DropMenu4FEB2018_FINAL.Models
{
    using System;

    public partial class DisplayCars_Result
    {
        public string DisplayList { get; set; }
        public string DisplayIndex { get; set; }
    }
}

I am unable to determine the syntax to replace the table reference with the stored procedure reference from within the controller method Create (the changes below don’t work)

 public ActionResult Create()
        {
            //DROPMENUEntities db = new DROPMENUEntities();
            //List<DropTest> list = db.DropTests.ToList();
            //ViewBag.DropTestList = new SelectList(list, "DisplayIndex", "DisplayList");

            DROPMENUEntities DROPMENUEntities = new DROPMENUEntities();
            List<DisplayCars_Result> list = DROPMENUEntities.DisplayCars.ToList();
            ViewBag.DisplayCars = new SelectList(list, "DisplayIndex", "DisplayList");

            return View();
        }

Any help will be gratefully received. Thanks.

1

1 Answers

0
votes

This was a simple fix to the List statement, that is, postfix the stored procedure name DisplayCars with (), see below:

public ActionResult Create()
{


    DROPMENUEntities db = new DROPMENUEntities();
    List<DisplayCars_Result> list = db.DisplayCars().ToList();
    ViewBag.DisplayCars = new SelectList(list, "DisplayIndex", "DisplayList");




    return View();
}

Now its working as expected. Cheers.