0
POSTに正しくリストされた複雑なオブジェクトを取得するために何度も試しました。投稿オブジェクトを除くすべてのプロパティには、投稿時の値が含まれます。私はこのSO Q&AとこのSO Q&Aに私のアプローチを基づいています。しかし、いずれの場合も、foreachループの代わりにforループを使用するという解決策があります。私は推奨されたforループを使用していますが、まだ問題はありません。投稿時にリストがnullになる原因が他に何であるかはわかりません。あなたの助けを前もってありがとう!MVC Razor POSTリストオブジェクトは常にnullです
ビュー:
@model PropertiesAdminSite.Models.UtilityData
@{
ViewBag.Title = "CreateNewCycle";
}
<h2>New Residence Utilities</h2>
@using (Html.BeginForm("Upload", "ImportWater", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="control-group">
@Html.TextBoxFor(m => m.UploadFile, new { type = "file"})
</div>
<div class="control-group">
<input type="submit" class="btn btn-info" value="Upload" />
</div>
<div class="col-lg-12 visible-lg">
<br>
<span style="color:green">@ViewBag.Message</span>
@Html.HiddenFor(model => model.bID)
@Html.HiddenFor(model => model.bEntryDate)
@Html.HiddenFor(model => model.bPrevDate)
@for (int i = 0; i < Model.utilData.Count(); i++)
{
@Html.HiddenFor(model => model.utilData[i].ResNumber)
@Html.HiddenFor(model => model.utilData[i].GrnLower)
@Html.HiddenFor(model => model.utilData[i].GrnUpper)
@Html.HiddenFor(model => model.utilData[i].prevWaterReading)
@Html.HiddenFor(model => model.utilData[i].rID)
@Html.HiddenFor(model => model.utilData[i].WaterReading)
@Html.HiddenFor(model => model.utilData[i].wDifference)
@Html.HiddenFor(model => model.utilData[i].YelLower)
@Html.HiddenFor(model => model.utilData[i].YelUpper)
}
</div>
}
@using (Html.BeginForm("IndexMulti", "Utilities", FormMethod.Post))
{
@Html.AntiForgeryToken()
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
@Html.LabelFor(model => model.bEntryDate, htmlAttributes: new { @class = "control-label col-md-1" })
@Html.DisplayFor(model => model.bEntryDate)
@Html.HiddenFor(model => model.bID)
@Html.HiddenFor(model => model.bEntryDate)
@Html.HiddenFor(model => model.bPrevDate)
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="dataTable_wrapper">
<!--div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">-->
<div class="row">
<div class="col-sm-12">
<table class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-Bills" role="grid" aria-describedby="dataTables-example_info">
<!-- /table headers-->
<thead>
<tr role="row">
<th>@Html.DisplayNameFor(model => model.utilData.First().ResNumber)</th>
<th>@Html.DisplayNameFor(model => model.utilData.First().WaterReading)</th>
<th>
@Html.DisplayNameFor(model => model.utilData.First().prevWaterReading)
@Html.DisplayFor(model => model.bPrevDate)
</th>
<th>@Html.DisplayNameFor(model => model.utilData.First().wDifference)</th>
<th>Actions</th>
</tr>
</thead>
<!-- /table body-->
<tbody>
@for (int i = 0; i < Model.utilData.Count(); i++)
{
<tr role="row">
<td>
@Html.DisplayFor(modelItem => modelItem.utilData[i].ResNumber)
@Html.HiddenFor(model => model.utilData[i].GrnLower)
@Html.HiddenFor(model => model.utilData[i].GrnUpper)
@Html.HiddenFor(model => model.utilData[i].YelLower)
@Html.HiddenFor(model => model.utilData[i].YelUpper)
</td>
<td>
@Html.EditorFor(model => model.utilData[i].WaterReading)
</td>
<td>
<span id="@string.Format("prevWater_{0}",Model.utilData[i].rID)">
@Html.DisplayFor(model => model.utilData[i].prevWaterReading)
</span>
@Html.HiddenFor(model => model.utilData[i].prevWaterReading)
</td>
<td>
<span id="@string.Format("hdifference_{0}",Model.utilData[i].rID)">
@Html.DisplayFor(model => model.utilData[i].wDifference)
</span>
@Html.HiddenFor(model => model.utilData[i].prevWaterReading)
</td>
<td>
@Html.ActionLink("View History", "ExportDataIndex", "ExportData", new { rID = Model.utilData[i].rID, bId = Model.bID }, null) |
<a href="@Url.Action("ExportToExcel", "ExportData", new { rID = Model.utilData[i].rID, bId = Model.bID })" class="btn btn-success">
<i class="fa fa-file-excel-o" aria-hidden="true" title="Export to Excel"></i>
</a> |
<a href="@Url.Action("ChartData", "Utilities", new { rID = Model.utilData[i].rID, bId = Model.bID })" class="btn btn-info">
<i class="fa fa-bar-chart" aria-hidden="true" title="Water Usage History"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-0 col-md-8">
<input type="submit" value="Submit Water Readings and Create Invoices" class="btn btn-primary btn-lg btn-block" />
</div>
</div>
}
モデル:
public partial class UtilityData
{
public DateTime bEntryDate { get; set; }
public string bPrevDate { get; set; }
public int bID { get; set; }
//public int residenceCount { get; set; }
public List<UtilEntry> utilData { get; set; }
public HttpPostedFileBase UploadFile { get; set; }
}
public partial class UtilEntry
{
public int rID { get; set; }
public long? WaterReading { get; set; }
public int ResNumber { get; set; }
public long? prevWaterReading { get; set; }
public decimal wDifference { get; set; }
public int GrnUpper { get; set; }
public int GrnLower { get; set; }
public int YelUpper { get; set; }
public int YelLower { get; set; }
}
コントローラー:@Stephen Mueckeパー
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult IndexMulti([Bind(Include = "bEntryDate, bPrevDate,bID,uData")] UtilityData uData)
{
if (ModelState.IsValid)
{
Omitted for clarity
}
return RedirectToAction("Create", "TenantAccount", new { id = uData.bID});
}
あなたの '[Bind]'属性は 'utilData'をバインディングから除外します! (ただし、そのデータをすべてビューに送信してからコントローラに戻すのはナンセンスです。編集が必要な場合はフォームコントロールを生成し、POSTメソッドでは他のデータを取得する場合は –
これがトリックでした。私はPOSTがモデルをミラーリングしなければならないと考えていましたが、返されるデータの量を減らそうとしています。 – PvSyemya
私はオーバーポストのために '[Bind]'をチェックするのを忘れたこの問題も抱えていました。 – kimbaudi