在ASP.NET MVC中渲染局部视图

时间:2015-12-13 06:44:43

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

我的代码如下。当我运行代码时,我可以看到ViewBag变量中的所有数据。但是在浏览器上渲染时,我看到了空白页面。可能是什么原因?代码有什么问题吗?请帮我。谢谢。

//控制器代码

 public class BlockController : Controller
  {

    [Authorize]
    [HttpGet]
    public ActionResult Index()
    {
        BlockViewModel model = new BlockViewModel();

        ViewBag.Facilities = new List<FieldProcessingFacility>();
        ViewBag.Data = new List<BlockField>();
        ViewBag.BlockFields = new List<BlockField>();
        ViewBag.Tanks = new List<FacilityTank>();
        ViewBag.Facility = new List<FieldProcessingFacility>();
        ViewBag.Blocks = model.GetAllBlocks();
        ViewBag.BlockPartial = new Epsa.Client.ViewModel.Master.Blocks.BlockViewModel();
        ViewBag.BlockField = new Epsa.Client.ViewModel.Master.Blocks.BlockViewModel();
        ViewBag.BlockFacility = new Epsa.Client.ViewModel.Master.Blocks.BlockFacilityViewModel();
        ViewBag.BlockTanks = new Epsa.Client.ViewModel.Master.Blocks.BlockTanksViewModel();
        return View(model);
    }


    [HttpPost]
    public PartialViewResult GetDetailsOfBlock(long blockID)
    {
        // To get fields

        BlockFacilityViewModel bkvm = new BlockFacilityViewModel();
        var fields = bkvm.BlockFieldList(blockID);
        ViewBag.Data = fields;

        // To get facilities
        BlockViewModel bvm = new BlockViewModel();
        var facilities = bvm.GetFacilitiesByBlock(blockID);
        ViewBag.Facility = facilities;

        // To get Tanks
        BlockViewModel blockModel = new BlockViewModel();
        var fieldtanks = blockModel.GetTanksByBlockID(blockID);
        ViewBag.Tanks = fieldtanks;

        BlockSingleViewModel blockSingleModel = new BlockSingleViewModel();
        ViewBag.Types = blockSingleModel.GetBlockTypes();


        //return RedirectToAction("Index");
        return PartialView("_PartialBlock");
    }
}

//我的部​​分视图

@model Epsa.Client.ViewModel.Master.Blocks.BlockViewModel

<div class="main mainDivPad">
<div class="col-lg-12" id="panel" style="padding: 0 !important">
    <div class="panel panel-primary">
        @*       Fields        *@
        <div class="panel-heading">
            <h1 class="panel-title" style="color: white">Fields & Tanks</h1>
        </div>
        <div class="row margintop20">
            <div class="col-lg-4 col-lg-offset-4">
                <div class="col-lg-8">
                    @Html.Label("Add Field Name")
                    <input type="text" id="fname" class="form-control dropDownLabel" placeholder="field Name" />
                </div>

                <div class="col-lg-4" style="margin-top: 30px">
                    <button type="button" class="btn btn-primary" id="Button1" onclick="saveFields()">Add</button>
                </div>
            </div>

            <div class="table-responsive col-lg-12 margintop20">
                <table class="table table-bordered table-hover" id="myTableDataField">
                    <thead>
                        <tr>
                            <th>Field Name</th>
                            <th style="width: 5%;">Edit</th>
                            <th style="width: 5%;">Delete</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{
                            var fielddata = ViewBag.Data as List<Epsa.Models.BlockField>;
                            foreach (var item in fielddata)
                            {
                            <tr>
                                <td>@item.Name</td>
                                <td>
                                    <button type="button"class="btn btn-md btn-primary newblock" id="editField"  value="@item.ID" onclick="getField(this)"><span class="glyphicon glyphicon-edit"></span></button>
                                </td>
                                <td>
                                    <button type="button"class="btn btn-md btn-primary newblock" value="@item.ID" onclick="deleteField(this)"><span class="glyphicon glyphicon-trash"></span></button>
                                </td>
                            </tr>
                            }
                        }
                    </tbody>
                </table>
            </div>

            @*      Facilities       *@

            <div class="col-lg-12">
                <div class="panel panel-primary">

                    <div class="col-lg-3 col-lg-offset-1 margintop20">
                        @Html.Label("Field Name")
                        @Html.DropDownListFor(m => m.BlockFieldID, new SelectList(ViewBag.Data as List<Epsa.Models.BlockField>, "ID", "Name"), "--Select--", new { @class = "form-control dropDownLabel", @id = "fcFID" })
                    </div>

                    <div class="col-lg-3 margintop20">
                        @Html.Label("Add Facility Name")
                        <input type="text" id="fcName" class="form-control dropDownLabel" placeholder="Facility Name" />
                    </div>

                    <div class="col-lg-3 margintop20">
                        @Html.Label("Facility Type")
                        <select class="form-control dropDownLabel" id="fID">
                            <option value="">--Select--</option>
                            <option value="1">CPF</option>
                            <option value="2">FPF</option>
                        </select>
                    </div>

                    <div class="col-lg-2" style="margin-top: 50px">
                        <button type="button" class="btn btn-primary" id="facilityAddEdit" onclick="saveFieldFacility()">Add</button>
                    </div>

                </div>
            </div>

            <div class="table-responsive col-lg-12 margintop20">
                <table class="table table-bordered table-hover" id="myTableDataFacility">
                    <thead>
                        <tr>
                            <th>Facility Name</th>
                            <th>Facility Type</th>
                            <th style="width: 2%;">Edit</th>
                            <th style="width: 2%;">Delete</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{
                            var facilitydata = ViewBag.Facility as List<Epsa.Models.FieldProcessingFacility>;
                            if (facilitydata.Count != 0)
                            {
                                foreach (var item in facilitydata)
                                {
                                    String type = "";
                                <tr>
                                    <td>@item.Name</td>
                                    @if (item.TypeID == 1)
                                    {
                                        type = "CPF";
                                    }
                                    else
                                    {
                                        type = "FPF";
                                    }
                                    <td>@type</td>
                                    <td>
                                        <button type="button"class="btn btn-md btn-primary newblock"  value="@item.ID" onclick="getFacility(this)"><span class="glyphicon glyphicon-edit"></span></button>
                                    </td>
                                    <td>
                                        <button type="button"class="btn btn-md btn-primary newblock" id="@item.BlockFieldID" value="@item.ID" onclick="deleteFacility(this)"><span class="glyphicon glyphicon-trash"></span></button>
                                    </td>
                                </tr>
                                }
                            }
                        }
                    </tbody>
                </table>
            </div>

            @*            Tanks            *@


            <div class="main col-lg-12" style="padding-top: 20px;">
                <div class="panel panel-primary">
                    <div class="col-lg-12 col-lg-offset-1">
                        @* <div class="col-lg-3">
                                                        @Html.Label("Field")
                                                        @Html.DropDownListFor(m => m.BlockFieldID, new SelectList(Model.BlockFieldsList, "ID", "Name"), "--Select--", new { @class = "form-control dropDownLabel", @id = "tankFID" })
                                                    </div>*@

                        <div class="col-lg-3 margintop20">
                            @Html.Label("Field Name")
                            @Html.DropDownListFor(m => m.BlockFieldID, new SelectList(ViewBag.Data as List<Epsa.Models.BlockField>, "ID", "Name"), "--Select--", new { @class = "form-control dropDownLabel", @id = "tankFID" })
                        </div>

                        <div class="col-lg-3 margintop20">
                            @Html.Label("Tank Name")
                            <input type="text" id="tname" class="form-control" placeholder="Tank Name" />
                        </div>
                        <div class="col-lg-3 margintop20">
                            @Html.Label("Capacity")
                            <input type="text" id="capa" class="form-control" placeholder="Capacity" />
                        </div>
                        <div class="col-lg-2" style="margin-top: 45px">
                            <button type="button" class="btn btn-primary" style="clear: right !important" onclick="saveFieldsTank()">Add</button>
                        </div>
                    </div>

                    <div class="table-responsive col-lg-12" style="padding-top: 20px;">
                        <table class="table table-bordered table-hover" id="myTableData">
                            <thead>
                                <tr>
                                    <th>Facility Name</th>
                                    <th>Tank</th>
                                    <th>Capacity</th>
                                    <th style="width: 2%;">Edit</th>
                                    <th style="width: 2%;">Delete</th>
                                </tr>
                            </thead>
                            <tbody>
                                @{
                                    var tankdata = ViewBag.Tanks as List<Epsa.Models.FacilityTank>;
                                    if (tankdata.Count != 0)
                                    {
                                        foreach (var itemvalue in tankdata)
                                        {
                                            if (itemvalue.IsDeleted == 1)
                                            {
                                            }
                                            else
                                            {
                                        <tr>
                                            <td>@itemvalue.Name</td>
                                            <td>@itemvalue.Volume</td>
                                            <td>
                                                <button type="button"class="btn btn-md btn-primary newblock" value="@itemvalue.ID" id="editTankData" onclick="editTankValue(this)"><span class="glyphicon glyphicon-edit"></span></button>
                                            </td>
                                            <td>
                                                <button type="button"class="btn btn-md btn-primary newblock" id="@itemvalue.ID" value="@itemvalue.ID" onclick="deleteTank(this)"><span class="glyphicon glyphicon-trash"></span></button>
                                            </td>
                                        </tr>
                                            }
                                        }
                                    }
                                }
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

  1. 您必须使用PartialView类型参数调用BlockViewModel函数。在这个时候你的部分参数显示为空。
  2. enter image description here

    return PartialView("_PartialBlock",blockModel);