How to link dropdownlist to partial view in mvc 4 using model?

Hi, I am using a partial view on the index.cshtml page. and a partial view contains one drop-down list that is dynamically linked to the database.

I do not want to use viewbag or viewdata to bind to this dropdown.

In the controller

    // GET: Reporting/Home
    public ActionResult Index()
    {
        var view = View();

        return view;
    }

    public ActionResult _ReportingMenu()
    {
        var DashboardList = GetAllDashboards();

        return View(DashboardList);
    }


    public IEnumerable<DashboardDefinition> GetAllDashboards()
    {
        return _reortDefinitionService.GetAllDashboards();
    } 

In index.cshtml

@model IEnumerable<Portal.Domain.ReportingModule.ReportDefinition.DashboardDefinition>

@{
    ViewBag.Title = "Reporting";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Reporting View</h2>
@Html.ActionLink("Add new Chart", "Create", "Chart", new { area = "Reporting" }, null)

@Html.Partial("_ReportingMenu")

In Partialview

    @model  Portal.Domain.ReportingModule.ReportDefinition.DashboardDefinition

    <div class="nav navbar navbar-default navbar-no-margin submenu">

        @Html.DropDownList("Mobiledropdown1",IEnumerable<Model.DashboardName>)   

        <a id="SubMenuIntegratorNew" class="btn btn-default submenu-item" href="#">
            <i class="fa fa-file-o fa-lg"></i>
            <span class="submenu-item-text">New</span>
        </a>
        <a id="SubMenuIntegratorSave" class="btn btn-default submenu-item" href="#">
            <i class="fa fa-floppy-o fa-lg"></i>
            <span class="submenu-item-text">Save</span>
        </a>
        <a id="SubMenuIntegratorSaveAs" class="btn btn-default submenu-item" href="#">
            <i class="fa fa-files-o  fa-lg"></i>
            <span class="submenu-item-text">Save As</span>
        </a>
        <a id="SubMenuIntegratorAddChart" class="btn btn-default submenu-item" href="#">
            <i class="fa fa-picture-o fa-lg"></i>
            <span class="submenu-item-text">Add Chart</span>
        </a>
    </div>

but I get a null model error.

I do not know how to implement in partial view

+4
source share
1 answer

You must use a view model, so you can contain both a list and a selected property in it, for example:

public class DashboardViewModel
{
    public IEnumerable<SelectListItem> Data { get; set; }
    //Might not be an int, but just an example
    public int SelectedId { get; set; }
}

Then fill it into the controller:

public ActionResult _ReportingMenu()
{
    var DashboardList = GetAllDashboards();

    var dropdownData = DashboardList
        .Select(d => new SelectListItem
        {
            Text = d.Name, //Need to apply the correct text field here
            Value = d.Id.ToString() //Need to apply the correct value field here
        })
        .ToList();

    var model = new DashboardViewModel
    {
       Data = dropdownData 
    };

    return View(model);
}

:

 @model DashboardViewModel

:

 @Html.DropDownListFor(m => m.SelectedId, Model.Data)
+4

Source: https://habr.com/ru/post/1584180/


All Articles