I have my view here and it throws errors, either there is missing {} block or an "eternal components throws and exception" when I remove those if/else block along with @: the code works just like before so how do I include html code in a C# block and C# code in html block in the following view:
@model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
@{
if (Model.Count < 1)
{
@:<p style="color:red">Item not found</p>
}
else
{
@:<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
foreach (var item in Model) {
@:<tr style="border:1px solid #bbb9b9;">
@: <td style="width:177px;">
@: <img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
@:</td>
@:<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
@Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
@: </span>
@:<span style="font-size:small">By @Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
@:<span style="font-size:x-small">
@Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: @Html.DisplayFor(modelItem => item.StockCount)
</span><br />
@using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: @Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
@:</table>
}
}
Here is the original code that threw the error
External component has thrown an exception.
Here is the original code
@model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
@if (Model.Count < 1)
{
<p style="color:red">Item not found</p>
}
else
{
<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
@foreach (var item in Model) {
<tr style="border:1px solid #bbb9b9;">
<td style="width:177px;">
<img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
</td>
<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
@Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
</span>
<span style="font-size:small">By @Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
<span style="font-size:x-small">
@Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: @Html.DisplayFor(modelItem => item.StockCount)
</span><br />
@using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: @Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
</table>
}
In the Index view it calls the partial page "_SearchResultList" here is the video that shows all

@:code? None of those seem necessary.@:IEnumerable<T>does not have a propertyCountso yourifblock throws an exception. Use@if (Model.Count() < 1)instead (of@if (Model.Any()). And remove all the@:code.