Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:57:47+00:00 2026-05-23T01:57:47+00:00

I have a server side object. public class OptionRelation { public intOptionId { get;

  • 0

I have a server side object.

public class OptionRelation
{
    public intOptionId { get; set; }
    public string Type { get; set; }
    public string Child { get; set; }
    public string Peer { get; set; }
}

Inside of my view, I do the following:

//where relations is a List<OptionRelation>
var a = relations.FindAll(r => r.OptionId == option.OptionID);
string data_relation = "";
if(a.Count > 0)
{
    data_relation = "data-relation=" + Json.Encode(a);
}

<input type="checkbox" @data_relation />

Attribute @data gets populated the way I expect most of the time. However, sometimes it breaks. If I open it in FireBug, the attributes of the <input> are all garbage. The only thing I can think of is that when it breaks the length of data-relation is slightly longer then the rest of the cases. Particularly, it breaks when data-relation should be:

data-relation="[{"OptionId":80,"Type":"required_1","Child":"#1625, #1626, #1627","Peer":""}]" 

Any ideas why this is breaking?

SOLUTION: I ended up rewriting tons of code and finally getting it to work using custom HTML helper and partially utilizing Darin’s code for HTML helper.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:57:47+00:00Added an answer on May 23, 2026 at 1:57 am

    You need to use Html.Raw in the attribute to avoid Razor performing double HTML encoding, just like this:

    var a = relations.FindAll(r => r.OptionId == option.OptionID);
    string data_relation = "";
    if(a.Count > 0)
    {
        data_relation = string.Format(
            "data-relation=\"{0}\"", 
            Html.AttributeEncode(Json.Encode(a))
        );
    }
    
    <input type="checkbox" @Html.Raw(data_relation) />
    

    This being said, writing so much C# code in a view is probably one of the worst things you could do in an ASP.NET MVC application. It’s a horrible to look and maintain. That’s not what views are supposed to do. That’s what custom HTML helpers might be useful. So why spaghettifying your views when you can simply write the following

    @Html.MyCheckBox(option.OptionID)
    

    That kind of looks better compared to the previous abomination, doesn’t it?

    And here’s what I mean by the custom helper:

    public static class CheckBoxExtensions
    {
        public static MvcHtmlString MyCheckBox(
            this HtmlHelper<IEnumerable<OptionRelation>> htmlHelper, 
            int optionId
        )
        {
            var checkbox = new TagBuilder("input");
            checkbox.Attributes["type"] = "checkbox";
            var model = htmlHelper.ViewData.Model;
            var a = model.Where(x => x.OptionId == optionId);
            if (a.Count() > 0)
            {
                var json = new JavaScriptSerializer().Serialize(a);
                checkbox.Attributes["data-relation"] = json;
            }
            return MvcHtmlString.Create(checkbox.ToString(TagRenderMode.SelfClosing));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple object that is deserialized from JSON into a server-side object.
In GWT 1.7 I have a class used to construct object on the server
I have a server side ImageButton, and a server side Button, and then a
I'm trying to adress the following issue: I have a server side .net application
I have to maintain a server-side script written in JScript (NOT Javascript) that needs
I currently have a fairly robust server-side validation system in place, but I'm looking
I have done a bit of testing on this myself (During the server side
I have some dynamically created inputs which are not server-side controls. I want to
I have a component which writes/generates javascript from a server side renderer. This component
I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.