【发布时间】:2015-12-13 14:58:40
【问题描述】:
我有如下代码。当我运行代码时,我可以看到 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>
【问题讨论】:
-
当你有模型的时候,你到底为什么要使用
ViewBag。那部分视图是什么 -Index.cshtml或_PartialBlock.cshtml? -
@StephenMuecke _PartialBlock 是部分视图。我对所有下拉列表和所有字段都使用了强类型模型。我使用单个控制器调用从 db 检索数据,并将它们存储在 ViewBag 变量中以在视图中使用。这不是正确的标准吗?
-
绝对不是(使用视图模型并使用
HtmlHelper方法强绑定到您的视图模型属性)。但是您希望在哪里看到这种观点?您展示的Index()方法的意义何在 - 它似乎与您的问题无关?您如何将数据发布到您的GetDetailsOfBlock()以返回此部分视图?
标签: c# html asp.net-mvc-4