2012-04-27 172 views
0

新剃刀,试图转换如下:MVC 4 ASPX剃刀

   <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; 

在此先感谢。

回答

0

你需要

@{ 
    string[] provinces = ViewBag.ProvincesForSelectedCountry; 

然后</select>标签前的收盘}

+0

感谢安德鲁,现在我发现了另一个错误:@Dictionary countryCodeNamePairs = ViewBag.CountryCodeNameDictionary;字符串是无效的表达! – hncl 2012-04-27 06:03:40

+0

再次尝试使用大括号。 – 2012-04-27 06:18:33

+0

再次感谢安德鲁。 – hncl 2012-04-27 06:30:09