0

Razor 新手,尝试转换以下内容:

                <select id="Province" name="Province" style="width: 235px; background-color: #FFFFCC;">
                    <%
       string[] provinces = ViewBag.ProvincesForSelectedCountry;
       string selectedProvinceName;
       if (Model != null && !String.IsNullOrEmpty(Model.Province))
           selectedProvinceName = Model.Province;
       else
           selectedProvinceName = ConfigData.DefaultProvinceName;

       foreach (var anEntry in provinces)
       {
           string selectedTextMark = anEntry == selectedProvinceName ? " selected=\"selected\"" : String.Empty;
                    %>
                    <option value="<%= anEntry %>" <%= selectedTextMark %>>
                        <%= anEntry %></option>
                    <%
       }
                    %>
                </select>
            </td>

剃须刀:

            <select id="Province" name="Province" style="width: 235px; background-color: #FFFFCC;">
                @string[] provinces = ViewBag.ProvincesForSelectedCountry;
   string selectedProvinceName;
   if (Model != null && !String.IsNullOrEmpty(Model.Province))
       selectedProvinceName = Model.Province;
   else
       selectedProvinceName = ConfigData.DefaultProvinceName;

   foreach (var anEntry in provinces)
   {
       string selectedTextMark = anEntry == selectedProvinceName ? " selected=\"selected\"" : String.Empty;

                <option value="@anEntry" @selectedTextMark>
                    @anEntry</option>

   }

            </select>
        </td>

我收到此错误:

Invalid expression term 'string' in  @string[] provinces = viewBag.ProvincesForSelectedCountry;

提前致谢。

4

1 回答 1

0

你需要

@{
    string[] provinces = ViewBag.ProvincesForSelectedCountry; 

然后在标签}之前关闭。</select>

于 2012-04-27T05:55:39.220 回答