0

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.

enter image description here

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

8
  • Why do you have all those @: code? None of those seem necessary. Commented Dec 6, 2015 at 4:52
  • @StephenMuecke i was experimenting : ) Commented Dec 6, 2015 at 4:52
  • @StephenMuecke the initial error occured when i inject an if/else block it thrown the error "External Component throws an exception" so i i tried some @s to it Commented Dec 6, 2015 at 4:54
  • I can't see any thing in your view that would necessitate the need for using @: Commented Dec 6, 2015 at 4:56
  • 1
    You should put a breakpoint in the partial view :) IEnumerable<T> does not have a property Count so your if block throws an exception. Use @if (Model.Count() < 1) instead (of @if (Model.Any()). And remove all the @: code. Commented Dec 6, 2015 at 5:18

1 Answer 1

1

None of your @: code is necessary. The reason you get the exception is that IEnumerable<T> does not have a property Count so your if block throws an exception. Change you code to

if (Model.Count() < 1)

of better, use

if (Model.Any())

and swap the code in the if and else blocks

Sign up to request clarification or add additional context in comments.

2 Comments

i want to extend this if you don't mind, i have partial view with a form that does ajax calls and the form is loaded to index view of another controller. Ajax sends data to my ajax action method but it does not place the return partial view of "_SearchResultList" on the index page. That updating div area is on the index page. are there any reasons why this fails?
Could be any number of reasons. You will need to ask a new question with the relevant code (including the script and the method that it calls). I'll keep a eye out for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.