0
我擁有如下代碼。當我運行代碼時,我可以看到ViewBag變量中的所有數據。但是,在瀏覽器上呈現時,我看到了空白頁面。可能是什麼原因?代碼中有什麼錯誤?請幫幫我。謝謝。在ASP.NET MVC中渲染局部視圖
//控制器代碼
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>
爲什麼在世界上使用'ViewBag'當你有一個模型。那是什麼部分視圖-'Index.cshtml'或'_PartialBlock.cshtml'? –
@StephenMuecke _PartialBlock是部分視圖。我對所有下拉菜單和所有字段都使用了強類型模型。我使用單個控制器調用從db中獲取數據,並將它們存儲在ViewBag變量中以在View中使用。這不是合適的標準嗎? –
絕對不是(使用視圖模型並使用'HtmlHelper'方法強制綁定到視圖模型屬性)。但是,你期望看到這個觀點?你已經展示的'Index()'方法有什麼意義 - 它與你的問題似乎沒有關係?你如何發佈數據到你的'GetDetailsOfBlock()'返回這個部分視圖? –