如何显示两个局部模板查看Index.cshtml MVC3数据? [英] How to show two Partials view data on Index.cshtml MVC3?

查看:135
本文介绍了如何显示两个局部模板查看Index.cshtml MVC3数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了MVC3 Web应用程序,并创建了两个局部视图
一种具有控件中两个DropDownList中。
第二个具有的WebGrid从数据库显示的数据。

I have created a web application in mvc3 and created two partial views one having controls two dropdownlist. second having webgrid which shows data from database.

    partialview1.cshtml
    @model Mapping.Models.SecurityIdentifierMapping

    @using (Html.BeginForm("Mapping", "Home"))
    {
        @Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
        <br />    
        @Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
        <br />
        <button type="submit">Map</button>
    }

    partialview2.cshtml
    @model IEnumerable<Mapping.Models.SecurityIdentifierMapping>

    @{
        ViewBag.Title = "Mapping";
        WebGrid grid = null;
        if (Model.Count() > 0 ){
        grid = new WebGrid(source: Model,
                                defaultSort: "Id",
                                canPage: true,
                                canSort: true,
                                rowsPerPage:20);
        }
    }


    <h2>Mapping</h2>


    @if (grid != null)
    {
    @grid.GetHtml(
                    tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",
                    columns: grid.Columns(
                                                grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id })  @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
                                                grid.Column("PricingSecurityID"),
                                                grid.Column("CUSIP")
                                              )

                    )
    }
    <br />
    <p>
        @Html.ActionLink("Back", "Index")
    </p>

    in index.cshtml


    <div>
    @Html.Partial("_ControlsPartial",)
    </div>

    <div>
    @Html.Partial("_WebGridPartial")
    </div>

inside Indexcontroller.cs in Index()

     public ActionResult Index()
            {
//FOR POPULATE DROPDOWN
                //SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
                //objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
                //objModel.CUSIP = objRepository.GetCUSIP();
                //return View(objModel);

              //FOR  DISPLAY DATA IN WEBGRID
                return View(dbContext.SecurityIdentifierMappings);
            }

这里的问题是的WebGrid局部视图是有 @model IEnumerable的&LT; Mapping.Models.SecurityIdentifierMapping&GT;

controlpartilview 是有 @model Mapping.Models.SecurityIdentifierMapping

所以,我怎么可以创建一个VIEWMODEL.cs一类新的将有两款机型,我怎么能写在索引((),使该方法将填充下拉菜单还并显示DATA IN的WebGrid ALSO?

so HOW CAN I CREATE A VIEWMODEL.cs A NEW CLASS WHICH WILL HAVE BOTH MODELS AND HOW CAN I WRITE IT IN INDEX(() SO THAT THAT METHOD WILL POPULATE DROPDOWN ALSO AND SHOW DATA IN WEBGRID ALSO ?

推荐答案

为什么不只是创建一个包含两个属性的自定义视图模型类:

Why not just create a custom View Model class that contains two properties:

public class SecurityIdentifierMappingViewModel
{
    public IEnumerable<SecurityIdentifierMapping> MappingList {get; set; }
    public SecurityIdentifierMapping Mapping {get; set; }
}

然后就可以通过这个自定义视图模型的索引视图和相应的属性,因为每个谐音的视图模型

Then you can pass this custom view model to the Index view and the corresponding property as the view model of each of the partials

您索引行动,那么就会是这个样子:

Your Index action would then look something like this:

public ActionResult Index()
{
    // single mapping
    var mapping = new SecurityIdentifierMapping();
    maping.PricingSecurityID = objRepository.GetPricingSecurityID();
    mapping.CUSIP = objRepository.GetCUSIP();

    var viewModel = new SecurityIdentifierMappingViewModel
    {
        Mapping = mapping,
        MappingList = dbContext.SecurityIdentifierMappings.ToList()
    };

    return View(viewModel);
}

而在 Index.cshtml

@model SecurityIdentifierMappingViewModel

<div>
    @Html.Partial("_ControlsPartial", Model.Mapping)
</div>

<div>
    @Html.Partial("_WebGridPartial", Model.MappingList)
</div>

这篇关于如何显示两个局部模板查看Index.cshtml MVC3数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆