简体   繁体   English

如何使用MVC Razor生成SVG

[英]How To Generate SVG using MVC Razor

We am trying to make an SVG generated radio button control for my MVC3 application. 我们正在尝试为我的MVC3应用程序制作一个SVG生成的单选按钮控件。 Ideally, the control would be a partial view and we would pass in a model which would contain data on how to generate the SVG using Razor. 理想情况下,控件应该是局部视图,我们将传入一个模型,其中包含有关如何使用Razor生成SVG的数据。

We have tried to write the SVG into Razor normally, but we get nothing but compile errors. 我们试图将SVG正常地写入Razor,但是除了编译错误外什么也没有。

How can we generate SVG using MVC3 Razor? 我们如何使用MVC3 Razor生成SVG?

The error is: Expression Must Return a Value To Render 错误是: Expression Must Return a Value To Render

EDIT : The error is not coming from within the partial view, but when we call @Html.RenderPartial("SvgRadioButtonControl", ((IResponseLabeledItem)Model).ResponseOptions) it gives the error. 编辑 :错误不是来自局部视图,但是当我们调用@ Html.RenderPartial(“ SvgRadioButtonControl”,(((IResponseLabeledItem)Model).ResponseOptions)时,它将给出错误。

    @using SmartQWeb.Models.Entities
@model IEnumerable<ResponseOption>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="300"
   height="400"
   id="radio">
  <script type="application/ecmascript"><![CDATA[
      var innerCircleExpandedSize = 11;
      function ResponseOptionClicked(evt) {
          console.log('Response option clicked');

          // Remove circle in enabled element
          var enabledElem = document.getElementsByClassName('enabled')[0];
          if (enabledElem != undefined) {
              console.log('Removing a inner circle');
enabledElem.getElementsByClassName('response-innercircle')[0].setAttribute('r', 0);
              enabledElem.className.baseVal = enabledElem.className.baseVal.replace('enabled', 'disabled')
          }

          // Add circle in radio button
          console.log('Adding a inner circle');
evt.currentTarget.getElementsByClassName('response-innercircle')[0].setAttribute('r', innerCircleExpandedSize);
          evt.currentTarget.className.baseVal = evt.currentTarget.className.baseVal.replace('disabled', 'enabled');
      }
  ]]></script>
    <g id="base">
        @{
            int iteration = 1;
        }
        @foreach (ResponseOption option in Model)
        {
            <g id="response-never" class="response-option disabled" transform="translate(50,@{ (iteration++ * 1).ToString(); })" onclick="ResponseOptionClicked(evt)" fill="#ffffff">
                <circle class="response-outercircle" r="18" stroke="#000000" stroke-width="3" stroke-miterlimit="4" stroke-opacity="1" stroke-dasharray="none" />
                <circle class="response-innercircle" r="0" fill="#000000" stroke="#000000" />
                <text x="40"  y="6.5" font-size="1.5em" fill="blue" class="response-text">@option.Label</text>
            </g>
        }
    </g>
</svg>

Replace: 更换:

@Html.RenderPartial("SvgRadioButtonControl", ((IResponseLabeledItem)Model).ResponseOptions)

with: 与:

@{Html.RenderPartial("SvgRadioButtonControl", ((IResponseLabeledItem)Model).ResponseOptions);}

or if you prefer: 或者,如果您喜欢:

@Html.Partial("SvgRadioButtonControl", ((IResponseLabeledItem)Model).ResponseOptions)

Also the following expression in your partial just stinks: 另外,部分表达中的以下表达式很臭:

@{ (iteration++ * 1).ToString(); })

Didn't you mean: 你不是说:

@(iteration++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM