1

In the view, I have:

grid.Column("Complete", header: "Verified", format:  (item) => @Html.Raw(item.Complete))

item.complete is an integer. I created an extension method to help display the corresponding string value since there are only 3 possibilities. Here is the extension method:

   public static string CompleteValue(this int value)
    {

        switch (value)
        {
            case 0:
                return "Submitted";
            case 1:
                return "Verified - Incomplete";
            case 2:
                return "Verified - Complete";
        }
        //Default if we get this far
        return "Submitted";
    }

I don't know how to write the Razor code to make it work. I have tried putting the ".CompleteValue" several places in the code but nothing works. Does anyone have any suggestions?

Edit: This is the view:

@model IEnumerable<SendMailwithAttachment.Models.MailModel>
@using System.Activities.Statements
@using Hazards
@using SendMailwithAttachment.Models;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>My Hazards</h2>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
@{

    var dataContext = new HazardsDataContext();

    var getHazards1 = from m in dataContext.Mail
                     where (m.User == User.Identity.Name)
                     select new{m.casenum,m.ID,m.User,m.Date,m.To,m.name,m.Hazard,m.PictureDataAsByte,
                         m.Risk,m.AfterPicture,m.status,m.Complete};

    var getHazards = from n in getHazards1
        orderby n.Complete  // Order by the Completed (or Verified Status so that verified reports show up last)
        select n;

    var grid = new WebGrid(source: getHazards);

    if (getHazards.Count() > 0)
    {     

        @grid.GetHtml(columns: new[] {
        grid.Column("Casenum", header: "Case #"),
        grid.Column("Date", header: "Date"),
        grid.Column("Hazard", header: "Hazard"),
        grid.Column("Before",header: "Before",format:
        @<Text><img src="@Url.Content(item.PictureDataAsByte)" width="175" height="175" />
        </text>),
        grid.Column("Risk", header: "Risk"),
        grid.Column("After",header: "After",format:
        @<Text><img src="@Url.Content(item.AfterPicture)" width="175" height="175" />
        </Text>),
        grid.Column("status", header: "Status"),
        //grid.Column("Complete", header: "Verified", format:(item) => (item.Complete))
        grid.Column("Complete", header: "Verified", format:  (item) => @Html.CompleteValue(item.Complete))
        //grid.Column("Complete", header: "Verified", format: (item) => @Html.Raw("<input type='checkbox' " + ((item.Complete==0) ? "checked" : "") + " disabled='disabled' />")),

})

     }
    else
    {
  @Html.Raw("No Data Found")
    }
}
2
  • Do you want to create an HTMLHelper extension? Commented Jan 17, 2015 at 21:01
  • That would be fine. I just dont know how to approach this. Commented Jan 17, 2015 at 22:35

2 Answers 2

1

You need to add the namespace, C# needs this to pick up the extension.

using My.Namescape.ExtensionClass;

Put it right up on top of the Razor. Alternatively, you can add it to the Web.config file inside the Views folder. This way it is available to all Razor views.

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

Comments

0

You don't have to write the Razor code, if you create an HTMLHelper for it. This helper can be used as many times as you want. So you don't have to add that to your view every time.

The class I think you should add, will look as follow:

public static class MVCHelpers
{
    public static MvcHtmlString CompleteValue(this HtmlHelper htmlHelper, int value)
    {
        //create the html helper
        var builder = new TagBuilder("text");
        //Check value
        switch (value)
        {
            case 0:
                builder.SetInnerText("Submitted");
                break;
            case 1:
                builder.SetInnerText("Verified - Incomplete");
                break;
            case 2:
                builder.setInnerText("Verified - Complete");
                break;
            default:
                builder.setInnerText("Submitted");
        }
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
}

So now we created the HTMLHelper, lets go to the view. Added the following code to the top of your view:

@namespace YourNameSpace

Or if you want the HTMLHelper to be available to all your views, insert following line in your web.config:

<add namespace="YourNameSpace" />

Now change following line in your view:

grid.Column("Complete", header: "Verified", format:  (item) => @Html.Raw(item.Complete))

To:

grid.Column("Complete", header: "Verified", format:  (item) => @Html.CompleteValue(item.Complete))

You have now created your own HTMLHelper. I hope this solves your question. If you have more questions on how to get this to work. Please ask:)

Note: I typed this code without an IDE, so there could be some syntax errors in it.

12 Comments

We might be closer. But here is the error I get: Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<SendMailwithAttachment.Models.MailModel>>' has no applicable method named 'CompleteValue' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
Also, the setInnerText is SetInnerText for anyone that is using this answer.
@SDanks I changed setInnerText to SetInnerText, have been programming in Java for the last few weeks. What code is giving you the error of your first comment?
When it tries to run, it it gets a runtime error because the grid.column line has the @Html.CompleteValue underlined in red (at design time) and when I hover over it, it says Extension methods cannot be dynamically dispatched. I have no clue what it is saying. I changed the var to TagBuilder but that didnt help so I am at a loss to know how to even troubleshoot it. What are the dynamic arguments that the above error refers to?
@SDanks I get the feeling something goes wrong in the View, the Model or your controller, could you post the method in the Controller that is being called and all code from your view?
|

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.