简体   繁体   English

如何使用模型在mvc 4中部分视图中绑定下拉列表?

[英]How to bind dropdownlist in partial view in mvc 4 using model?

Hi i am using partial view in index.cshtml page. 嗨我在index.cshtml页面中使用局部视图。 and partial view contains one dropdownlist which is bind dynamically from database. 和部分视图包含一个从数据库动态绑定的下拉列表。

I don't want to use viewbag or viewdata for binding this dropdownlist. 我不想使用viewbag或viewdata来绑定此下拉列表。

In 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 在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 在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 an error of null model. 但是我得到了null模型的错误。

I don't know how to implement in partial view 我不知道如何在局部视图中实现

You should use a view model, so you can contain both the list and the selected property in it, something like: 您应该使用视图模型,因此您可以在其中包含列表和选定的属性,例如:

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

Then populate it in your 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);
}

Then in your both your views, set the model: 然后在您的两个视图中,设置模型:

 @model DashboardViewModel

Then in your partial you can do: 然后在你的部分你可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用MVC RAZOR将DataTable绑定到DropDownList? - How to bind a DataTable to a DropDownList using MVC RAZOR? 如何在MVC 3中使用实体框架绑定dropdownList - how to Bind dropdownList using Entity Framework in MVC 3 如何在部分视图中绑定嵌套模型 - How to bind nested model in partial view 如何将数据表绑定到模型,然后绑定到MVC中的dropdownlist? - How do I bind a datatable to model and then to a dropdownlist in mvc? 如何将IEnumerable模型传递给MVC中的局部视图 - How to pass an IEnumerable model to a partial view in MVC 如何将kendo下拉列表绑定到模型(强类型视图) - How to bind kendo dropdownlist to model (strongly typed view) 如何在MVC 3.0中绑定dropdownlist - How to bind dropdownlist in mvc 3.0 如何在MVC WebAPI中绑定下拉列表? - How to bind the dropdownlist in MVC WebAPI? 如何使用ASP MVC SQL和EF从同一视图中的差异模型创建两个DropDownList - How to create two DropDownList from differents model in the same view using ASP MVC SQL and EF 如何使用带有剃刀视图的实体框架(.edmx模型)为MVC4或MVC 5创建局部视图? - How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM