asp.net - 如何使用 MVC Razor 生成 SVG

标签 asp.net asp.net-mvc-3 razor svg

我们正在尝试为我的 MVC3 应用程序制作 SVG 生成的单选按钮控件。理想情况下,控件将是部分 View ,我们将传入一个模型,其中包含有关如何使用 Razor 生成 SVG 的数据。

我们尝试将 SVG 正常写入 Razor,但除了编译错误之外什么也没得到。

我们如何使用 MVC3 Razor 生成 SVG?

错误是:表达式必须返回要渲染的值

编辑:错误不是来自部分 View 内,而是当我们调用 @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>

最佳答案

替换:

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

与:

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

或者如果您愿意:

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

此外,您的部分中的以下表达式也很糟糕:

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

你的意思是不是:

@(iteration++)

关于asp.net - 如何使用 MVC Razor 生成 SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009054/

相关文章:

asp.net-mvc - 是否可以将封装 Person 的 ViewModel 对象传递给 Create View ,但仅从 POST Create 操作方法接收 Person 对象?

asp.net-mvc - 如何在 MVC 3 中将列表从 Controller 传递到 View

asp.net-mvc-4 - 未终止的字符串常量 MVC4 Razor View 引擎

asp.net - 启用分页时,Gridview 在回发时消失

asp.net - 值不能为空。参数名称 : value

c# - RedirectToAction 和 "Object moved to here"错误

c# - 为 WebGrid MVC 3 上的单列指定格式

javascript - URL 前缀 "res://"- 它有什么作用或者是什么创建了这个前缀?

javascript - 无法从母版页的代码页中调用 Web 方法

asp.net-mvc-3 - MVC3 HtmlHelper 返回 '