如何使用ASP.NET MVC在编辑视图中传递2个或多个模型?

时间:2017-04-08 15:41:32

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我上次发帖但我觉得我的问题不对。所以我明白了。目前,我正在研究一个项目我现在是项目的75%我在编辑视图中被堆叠了我只想在视图中传递2个或多个模型我查看模型但是它需要在调用之前进行迭代该类或模型的属性。我已经完成了,但我不知道如何在dropdownlistfor()中传递模型。

我想在下面的我需要的表格中实现一个编辑视图,在表格中它有一个dropdownlistfor()list1,bedroom2,bedroom3等等。

代码:

   public async Task<ActionResult> Edit(long? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var vm = await _context.DwPropertyMasters.FindAsync(id);

                if (vm == null)
                {
                    return HttpNotFound();
                }

                TempData["MapPointX"] = vm.MapPointX;
                TempData["MapPointY"] = vm.MapPointY;

                var vmMapPoint = new MapPointEditViewModels
                {
                    LandId = vm.LandId,
                    Location = vm.Location,
                    AreaSize = vm.AreaSize,
                    AreaSize2 = vm.AreaSize2,
                    Mgm = vm.Mgm,
                    Developer = vm.Developer,
                    MapPointX = vm.MapPointX,
                    MapPointY = vm.MapPointY,
                    ShowMapPoint = vm.ShowMapPoint,
                    BatchUpdate = vm.BatchUpdate,
                    Development = vm.Development,
                    Premium = vm.Premium,
                    LandLot = vm.LandLot,
                    LastModifiedBy = vm.LastModifiedBy,
                    LandTypeId = vm.LandTypeId,
                    LandTypes = _context.DwPropertyLandTypes.ToList(),
                    YearTender = vm.YearTender,
                    DownloadLinkRt1 = vm.DownloadLinkRt1,
                    DownloadLinkRt2 = vm.DownloadLinkRt2,

                };

                var vmDetails = _context.DwPropertyDetails.Where(y => y.LandId == id)
                    .Select(y => new
                    {
                        y.Block,
                        y.Floor,
                        y.Unit,
                        y.SalePrice,
                        y.TransactionPrice,
                        y.ActualSize,
                        y.FlatType
                    }).ToList();

                ViewData["Details"] = vmDetails.Select(x => new DwPropertyDetail
                {
                    Block = x.Block,
                    Floor = x.Floor,
                    Unit = x.Unit,
                    SalePrice = x.SalePrice,
                    TransactionPrice = x.TransactionPrice,
                    ActualSize = x.ActualSize,
                    FlatType = x.FlatType
                });

                return View(vmMapPoint);
            }

查看:

<div class="form-group">
    <table class="table table-bordered table-hover" style="font-size: smaller; text-align: center; width: 100%;" id="detailsDataTable">
        <thead style="background: #ecf0f1; color: #7f8c8d;">
            <tr class="cls-property">
                <th>No.</th>
                <th>Block</th>
                <th>Floor</th>
                <th>Unit</th>
                <th>Transaction Price</th>
                <th>Sale Price</th>
                <th>Size</th>
                <th>Flat Type</th>
            </tr>
        </thead>
        <tbody>
            @{ var counter = 0;}
            @foreach (var details in (IEnumerable<DwPropertyDetail>)ViewData["Details"])
            {
                <tr>
                    <td>@(++counter)</td>
                    <td>@Html.DisplayFor(model => details.Block)</td>
                    <td>@Html.DisplayFor(model => details.Floor)</td>
                    <td>@Html.DisplayFor(model => details.Unit)</td>
                    <td>@Html.DisplayFor(model => details.TransactionPrice)</td>
                    <td>@Html.DisplayFor(model => details.SalePrice)</td>
                    <td>@Html.DisplayFor(model => details.ActualSize)</td>
                    <td>@Html.EditorFor(model => details.FlatType, new { htmlAttributes = new { @class = "form-control input-sm" } })</td>
                </tr>
            }
        </tbody>
    </table>
</div>

3 个答案:

答案 0 :(得分:0)

我对这个答案犹豫不决,但它不能作为评论。我认为您正在寻找的是将您的对象包装在封装模型中。所以(我认为)你想构建一个这样的实例并将其传递给视图:

public class MyViewModel
{
    public MapPointEditViewModels MapPoint {get;set;}
    public List<DwPropertyDetails> Details {get;set;}

}

答案 1 :(得分:0)

从您的代码中,您似乎需要访问单个MapPointEditViewModels项和DwPropertyDetails个项目的集合。

您必须创建一个将这些项目放在一起的ViewModel类。像这样:

public class MapPointDWPropertyVM
{
    public MapPointEditViewModels MapPointEditViewModel{get;set;}
    public IEnumerable<DwPropertyDetails> DwPropertyDetailsList{get;set;}
}

在您的视图中访问此内容:

@foreach (var details in Model.DwPropertyDetailsList)
            {
                <tr>
                    <td>@(++counter)</td>
                    <td>@Html.DisplayFor(model => details.Block)</td>
                    <td>@Html.DisplayFor(model => details.Floor)</td>
                    <td>@Html.DisplayFor(model => details.Unit)</td>
                    <td>@Html.DisplayFor(model => details.TransactionPrice)</td>
                    <td>@Html.DisplayFor(model => details.SalePrice)</td>
                    <td>@Html.DisplayFor(model => details.ActualSize)</td>
                    <td>@Html.EditorFor(model => details.FlatType, new { htmlAttributes = new { @class = "form-control input-sm" } })</td>
                </tr>
            }

答案 2 :(得分:0)

您可以直接使用ViewData

ViewData:您在Microsoft Page了解详情。

这是最简单的方法,但如果您使用的是实体框架,请使用其他用户已建议的ViewModel