Hello All,
Im trying to bind a model with mutiple tables to a single view. There is 1:1, 1:N relationship between these tables. But Im getting error as
"foreach statement cannot operate on variables of type 'BOL.tbl_repair_history' because 'BOL.tbl_repair_history' does not contain a public definition for 'GetEnumerator'". Below is my code.
Model Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BOL
{
public class AllAssetInfo
{
public tbl_assets_master AssetInfo { get; set; }
public tbl_repair_history RepairInfo { get; set; }
}
}
View:
@model BOL.AllAssetInfo
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<h2>Asset Information</h2>
<hr />
@using (Html.BeginForm("UpdateAssetInfo","SearchAsset",FormMethod.Post))
{
@Html.AntiForgeryToken()
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#1" data-toggle="tab">Asset details</a></li>
<li><a href="#2" data-toggle="tab">Repair History</a></li>
<li><a href="#3" data-toggle="tab">Transfer History</a></li>
<li><a href="#4" data-toggle="tab">Related Documents</a></li>
</ul>
<br />
<div class="tab-content">
<div id="1" class="tab-pane fade active in" data-toggle="tab">
<div class="form-row">
<div class="form-group col-md-4">
<label for="it_code_old">Old IT Code</label>
@Html.EditorFor(model => model.AssetInfo.it_code_old, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AssetInfo.it_code_old, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-4">
<label for="baan_code">BAAN Code</label>
@Html.EditorFor(model => model.AssetInfo.baan_code, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AssetInfo.baan_code, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-4">
<label for="purchase_date">Purchase Date</label>
@Html.EditorFor(model => model.AssetInfo.purchase_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AssetInfo.purchase_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="unit_details">Unit Details</label>
@Html.EditorFor(model => model.AssetInfo.unit_details, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AssetInfo.unit_details, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-4">
<label for="category_id">Category</label>
@Html.DropDownList("category_id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AssetInfo.category_id, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-4">
<label for="sub_category_id">Sub-Category</label>
@*@Html.DropDownList("sub_category_id", Enumerable.Empty<SelectListItem>(), "--Select Parent--", new { @class="form-control"})*@
@Html.DropDownList("sub_category_id", null, htmlAttributes: new { @class = "form-control" })
@*<select id="sub_category_id" name="sub_category_id" class="form-control">
<option value="-1">Select Sub category</option>
</select>*@
@Html.ValidationMessageFor(model => model.AssetInfo.sub_category_id, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
<button type="submit" class="btn btn-primary">Update Info</button>
</div>
</div>
<div id="2" class="tab-pane fade" data-toggle="tab">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.it_code)
</th>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.repair_date)
</th>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.repair_issue)
</th>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.repair_cost)
</th>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.repair_company)
</th>
<th>
@Html.DisplayNameFor(model => model.RepairInfo.remarks)
</th>
<th></th>
</tr>
@foreach (var item in Model.RepairInfo)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.it_code)
</td>
<td>
@Html.DisplayFor(modelItem => item.repair_date)
</td>
<td>
@Html.DisplayFor(modelItem => item.repair_issue)
</td>
<td>
@Html.DisplayFor(modelItem => item.repair_cost)
</td>
<td>
@Html.DisplayFor(modelItem => item.repair_company)
</td>
<td>
@Html.DisplayFor(modelItem => item.remarks)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.repair_id }) |
@Html.ActionLink("Details", "Details", new { id = item.repair_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.repair_id })
</td>
</tr>
}
</table>
</div>
<div id="3" class="tab-pane fade" data-toggle="tab">
TAB-3
</div>
<div id="4" class="tab-pane fade" data-toggle="tab">
TAB-4
</div>
</div>
}
Controller:
public ActionResult Edit(int Id)
{
var AssetDetails = objBs.AssetInfoBs.GetById(Id);
int SubCategory = AssetDetails.sub_category_id;
var CategoryDetails = objBs.SubCategoryBs.GetCategoryIdForSubCategory(SubCategory);
int CategorId = CategoryDetails.category_id;
ViewBag.category_id = new SelectList(objBs.CategoryBs.GetAll().ToList(), "s_no", "main_category", CategorId);
ViewBag.sub_category_id = new SelectList(objBs.SubCategoryBs.GetAll().ToList(), "s_no", "sub_category", AssetDetails.sub_category_id);
return View(AssetDetails);
}