Results 1 to 8 of 8

Thread: Populate asp net mvc dropdownlist from a table in an existing model

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Populate asp net mvc dropdownlist from a table in an existing model

    I am new in mvc. I have a search page where there are some dropdown fields. These fields are populated from database tables. I am adding controllers and views to the model, do I need to make new connections again? I am getting an error message when I try to populate the dropdown list. Attached is the screenshot of my solution explorer, GetDeleteRecordsController.cs is the controller, GetDeleteRecords is the view folder, Model1.edmx is the existing model used by the project.
    Please help.
    Attached Files Attached Files

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    Please dont use attachments to show your problem, i cant download that attachment and many others just wont.

    I use MVC a lot so i am sure i could help you if you posted some code snippets instead, using the # code tags on the post editor toolbar
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    I am getting the following error message

    Compiler Error Message: CS1061: 'PagedList.IPagedList<InventoryDB.Models.invent>' does not contain a definition for 'Description1' and no extension method 'Description1' accepting a first argument of type 'PagedList.IPagedList<InventoryDB.Models.invent>' could be found (are you missing a using directive or an assembly reference?)

    Source Error:


    Line 71: <td>
    Line 72: <div class="form-group">
    Line 73: @Html.LabelFor(model => model.Description1, htmlAttributes: new { @class = "control-label col-md-2" })
    Line 74: <div class="col-md-10">
    Line 75: @Html.DropDownList("Description1", null, htmlAttributes: new { @class = "form-control" })


    Following is the portion of my Controller code, I am trying to populate the Description1 dropdown:


    public ViewResult Index(string sortOrder, int? page,
    string currDHRInventNo, string DHRInventNo,
    string currDescription1, string Description1,
    string currDescription2, string Description2,
    string currSerialNo, string SerialNo,
    string currPONo, string PONo,
    string currRoomNo, string RoomNo,
    string currUnit, string Unit,
    string currUniqueID, string UniqueID,
    string currInventNo, string InventNo,
    string currDatePicker, string DatePicker,
    string currDatePicker2, string DatePicker2
    )

    {
    ViewBag.CurrentSort = sortOrder;
    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

    //if (searchString != null)
    //{
    // page = 1;
    //}
    //else
    //{
    // searchString = currentFilter;
    //}

    if ((DHRInventNo != null) || (Description1 != null) || (Description2 != null) || (SerialNo != null) ||
    (PONo != null) || (RoomNo != null) || (Unit != null) || (InventNo != null) || (UniqueID != null) ||
    (DatePicker != null) || (DatePicker2 != null))
    {
    page = 1;

    }
    else
    {
    DHRInventNo = currDHRInventNo;
    Description1 = currDescription1;
    Description2 = currDescription2;
    SerialNo = currSerialNo;
    PONo = currPONo;
    RoomNo = currRoomNo;
    Unit = currUnit;
    InventNo = currInventNo;
    UniqueID = currUniqueID;
    DatePicker = currDatePicker;
    DatePicker2 = currDatePicker2;
    }

    ViewBag.CurrDHRInventNo = DHRInventNo;
    ViewBag.CurrDescription1 = new SelectList(db.tblDescription1, "Description1", "Description1");


    Following is the portion of my View code:

    @using (Html.BeginForm("Index", "invents", FormMethod.Get))
    {
    <p>

    <table>
    <tr>
    <td>
    <b>Search for:</b>
    </td>
    <td></td>
    </tr>
    <tr>

    <td>
    <b>Description:</b>
    </td>
    <td>
    <div class="form-group">
    @Html.LabelFor(model => model.Description1, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("Description1", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Description1, "", new { @class = "text-danger" })
    </div>
    </div>
    </td>
    </tr>

    Please help.
    Thanking you in advance

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    you are setting your viewbag to currdescription1 but the control is looking for description1. That may be the only issue.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    Changing viewbag & control to currdescription1 did not help, then I am getting following error message.

    Compiler Error Message: CS1061: 'PagedList.IPagedList<InventoryDB.Models.invent>' does not contain a definition for 'CurrDescription1' and no extension method 'CurrDescription1' accepting a first argument of type 'PagedList.IPagedList<InventoryDB.Models.invent>' could be found (are you missing a using directive or an assembly reference?)

    Source Error:


    Line 106: <td>
    Line 107: <div class="form-group">
    Line 108: @Html.LabelFor(model => model.CurrDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
    Line 109: <div class="col-md-10">
    Line 110: @Html.DropDownList("CurrDescription1", null, htmlAttributes: new { @class = "form-control" })

    Source File: c:\Users\spramanik\Documents\Visual Studio 2019\Projects\InventoryDB\InventoryDB\Views\GetDeleteRecords\Index.cshtml Line: 108

    Here is my Viewbag code

    @using PagedList.Mvc;
    @model PagedList.IPagedList<InventoryDB.Models.invent>
    @using PagedList.Mvc;

    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/jquery-1.12.4.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.12.1.js")"></script>


    @{
    ViewBag.Title = "Index";
    }


    <tr>
    <td>General Description:</td>
    <td>
    <div class="form-group">
    @Html.LabelFor(model => model.CurrDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("CurrDescription1", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CurrDescription1, "", new { @class = "text-danger" })
    </div>
    </div>
    </td>
    </tr>




    Controller code:

    ViewBag.CurrDescription1 = new SelectList(db.tblDescription1, "Description1", "Description1", Description1);

    What am I doing wrong?

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    Changing viewbag & control to currdescription1 did not help, then I am getting following error message.

    Compiler Error Message: CS1061: 'PagedList.IPagedList<InventoryDB.Models.invent>' does not contain a definition for 'CurrDescription1' and no extension method 'CurrDescription1' accepting a first argument of type 'PagedList.IPagedList<InventoryDB.Models.invent>' could be found (are you missing a using directive or an assembly reference?)

    Source Error:


    Line 106: <td>
    Line 107: <div class="form-group">
    Line 108: @Html.LabelFor(model => model.CurrDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
    Line 109: <div class="col-md-10">
    Line 110: @Html.DropDownList("CurrDescription1", null, htmlAttributes: new { @class = "form-control" })

    Source File: c:\Users\spramanik\Documents\Visual Studio 2019\Projects\InventoryDB\InventoryDB\Views\GetDeleteRecords\Index.cshtml Line: 108

    Here is my Viewbag code

    @using PagedList.Mvc;
    @model PagedList.IPagedList<InventoryDB.Models.invent>
    @using PagedList.Mvc;

    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/jquery-1.12.4.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.12.1.js")"></script>


    @{
    ViewBag.Title = "Index";
    }


    <tr>
    <td>General Description:</td>
    <td>
    <div class="form-group">
    @Html.LabelFor(model => model.CurrDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("CurrDescription1", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CurrDescription1, "", new { @class = "text-danger" })
    </div>
    </div>
    </td>
    </tr>




    Controller code:

    ViewBag.CurrDescription1 = new SelectList(db.tblDescription1, "Description1", "Description1", Description1);

    What am I doing wrong?

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    I do not understand the Compiler Error Message: CS1061: 'PagedList.IPagedList<InventoryDB.Models.invent>' does not contain a definition for 'CurrDescription1' and no extension method 'CurrDescription1' accepting a first argument of type 'PagedList.IPagedList<InventoryDB.Models.invent>' could be found (are you missing a using directive or an assembly reference?)

    Following is my PagedListPager code and db.tblDescription1 is in model.





    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

    @Html.PagedListPager(Model, page => Url.Action("Index",
    new
    {
    page,
    currDescription1 = ViewBag.CurrDescription1,

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Populate asp net mvc dropdownlist from a table in an existing model

    I do not understand the Compiler Error Message: CS1061: 'PagedList.IPagedList<InventoryDB.Models.invent>' does not contain a definition for 'CurrDescription1' and no extension method 'CurrDescription1' accepting a first argument of type 'PagedList.IPagedList<InventoryDB.Models.invent>' could be found (are you missing a using directive or an assembly reference?)

    Following is my PagedListPager code and db.tblDescription1 is in model.





    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

    @Html.PagedListPager(Model, page => Url.Action("Index",
    new
    {
    page,
    currDescription1 = ViewBag.CurrDescription1,

    I will appreciate somebody can help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width