Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 属性';数据';有时在渲染时中断_Json_Html_Asp.net Mvc 3_Razor - Fatal编程技术网

Json 属性';数据';有时在渲染时中断

Json 属性';数据';有时在渲染时中断,json,html,asp.net-mvc-3,razor,Json,Html,Asp.net Mvc 3,Razor,我有一个服务器端对象 public class OptionRelation { public intOptionId { get; set; } public string Type { get; set; } public string Child { get; set; } public string Peer { get; set; } } 在我看来,我做了以下几点: //where relations is a List<OptionRelati

我有一个服务器端对象

public class OptionRelation
{
    public intOptionId { get; set; }
    public string Type { get; set; }
    public string Child { get; set; }
    public string Peer { get; set; }
}
在我看来,我做了以下几点:

//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 />
你知道为什么会这样吗


解决方案:我最终重写了大量代码,并最终使用自定义HTML helper和部分使用Darin的HTML helper代码使其正常工作。

您需要在属性中使用
HTML.Raw
,以避免Razor执行双重HTML编码,如下所示:

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) />
与以前的令人憎恶的东西相比,这种东西看起来更好,不是吗

下面是我所说的自定义帮助器的意思:

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));
    }
}
公共静态类CheckBoxExtensions
{
公共静态MvcHtmlString MyCheckBox(
这个HtmlHelper HtmlHelper,
int optionId
)
{
var复选框=新标记生成器(“输入”);
checkbox.Attributes[“type”]=“checkbox”;
var model=htmlHelper.ViewData.model;
var a=模型,其中(x=>x.OptionId==OptionId);
如果(a.Count()>0)
{
var json=new JavaScriptSerializer().Serialize(a);
checkbox.Attributes[“数据关系”]=json;
}
返回MvcHtmlString.Create(checkbox.ToString(TagRenderMode.SelfClosing));
}
}

Html.AttributeEncode行抛出空引用异常。实际上我在App\u代码文件夹的@helper中有它,只是没有提到它。但是谢谢你指出!这行代码仍在崩溃:Html.attributencode(Json.Encode(a))对象为空。我重写了许多代码,并使用自定义Html帮助程序使其正常工作。答案中的第一个代码段引发“对象引用未设置为对象实例”异常。关于Html.Raw和Html.attributencode的内容不喜欢Json编码的字符串和空字符串。也许你应该把它从答案中去掉?
@Html.MyCheckBox(option.OptionID)
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));
    }
}