0

I post last time but I think my question is wrong. So I figured it out. Currently, I am working on a project right now I'm 75% of the project I get stacked in the edit view I just want to pass 2 or multiple models in a view I do view model but it needs to iterate before you call the properties of that class or the model. I've done that but I don't know how to pass a model in a dropdownlistfor().

I want to achieve a edit view below of that I need table and in the table it has a dropdownlistfor() list of bedroom1, bedroom2, bedroom3 and so on..

Code:

   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);
            }

View:

<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>
Tahir Alvi
  • 896
  • 2
  • 14
  • 44
  • 1
    You can only have one Model, so you may need to create a class that has a property for each Type you want to include. That way you can access multiple objects, but as properties of a single Model. – Crowcoder Apr 08 '17 at 15:45
  • @Crowcoder thanks for your reply. Do you have an example? – Patrick Pangilinan Apr 08 '17 at 15:48

3 Answers3

0

I hesitate to make this an answer but it would not work as a comment. What I think you are looking for is to wrap your objects in an encapsulating Model. So (I think) you want to build an instance like this and pass it to the view:

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

}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

From your code, looks like you need to access a single MapPointEditViewModels item and a collection of DwPropertyDetailsitems.

You will have to create a ViewModel class that holds these items together. Like this :

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

Access this in your view like this :

@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>
            }
Ranjith V
  • 298
  • 1
  • 3
  • 16
  • nice! thank you! but I want in the last must be a @Html.DropDownListFor() because this is list of category example Bedroom 1, Bedroom 2, Bedroom 3, and so on.. – Patrick Pangilinan Apr 08 '17 at 16:13
0

You can simply user ViewData for that.

ViewData : you learn more on Microsoft Page.

This is most simplest way, but if you are using entity framework then use the ViewModel already suggest by other users.

Tahir Alvi
  • 896
  • 2
  • 14
  • 44