2012-08-17 60 views
4

我們試圖爲我的MVC3應用程序生成一個SVG生成的單選按鈕控件。理想情況下,控件將是一個局部視圖,我們將傳入一個模型,該模型將包含關於如何使用Razor生成SVG的數據。如何使用MVC Razor生成SVG

我們試圖通常將SVG寫入Razor,但是我們只能編譯錯誤。

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

的錯誤是:Expression Must Return a Value To Render

編輯:該錯誤不從局部視圖內的,但是當我們調用@ Html.RenderPartial( 「SvgRadioButtonControl」,((IResponseLabeledItem)模型).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> 
+0

什麼編譯錯誤? – Shyju 2012-08-17 15:53:55

+0

你可以發佈一個示例視圖,哪裏出錯? – nemesv 2012-08-17 15:54:01

+0

錯誤的行號? – 2012-08-17 16:18:39

回答

6

替換:

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

有:

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

或者如果你喜歡:

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

而且在部分只是太臭下面的表達式:

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

沒有你的意思是:

@(iteration++) 
+0

不應該被稱爲正式的氣味,而不是臭味? ,P – Pascal 2017-12-18 19:22:13