ASP.NET MVC 2的奇怪行为

时间:2010-05-29 15:08:13

标签: asp.net asp.net-mvc-2

最近我安装了VS 2010 Release(从RC迁移),我的MVC应用程序不再工作了。更具体:我有一个向导,其中包含几个创建新客户帐户的步骤(Jquery表单向导,但这并不重要)。每个步骤都包含一个键入的部分视图,用于帐户的每个部分:公司,客户,许可证等。当我提交表单时,我在ModelState中看到了非常奇怪的事情。公司有重复的密钥:带有“公司”前缀而没有它。像这样:

  

[6]“Company.Phone”字符串
  [12]“电话”字符串

我的所有这些密钥的模型状态无效,因为公司实际上是null并且验证失败。当它是RC时,没有带有“公司”前缀的密钥。因此,在我安装VS Release之后,ModelState中带有前缀“Company”的这些键出现了。这是我的代码: 主视图

<div id="registerSteps">
        <div id="firstStep" class="step">  
         <fieldset>       
          <legend><%=Html.Encode(Register.CustomerInfo) %></legend>
           <% Html.RenderPartial("CustomerInfo", ViewData["newCust"]); %>          
         </fieldset>
        </div>
        <div id="secondStep" class="step"> 
         <fieldset>       
          <legend><%=Html.Encode(Register.CompanyInfo) %></legend>       
          <% Html.RenderPartial("CompanyInfo", ViewData["newComp"]); %>
          </fieldset>
        </div>
        <div id="thirdStep" class="step">  
        <fieldset>       
          <legend><%=Html.Encode(Register.LicenceInfo) %></legend>         
          <% Html.RenderPartial("LicenceInfo", ViewData["newLic"]); %>
          </fieldset>
        </div>
        <div id="lastStep" class="step">
         <fieldset>       
          <legend><%=Html.Encode(Register.PrivacyStatement) %></legend>   
          <% Html.RenderPartial("PrivacyStatementInfo"); %>
          </fieldset>
        </div>        
        <div id="registerNavigation">                           
           <input class="navigation_button" value="Back" type="reset"/> 
           <input class="navigation_button" value="Next" type="submit"/>            
        </div> 
       </div>

两个部分视图(显示它们实际上是相同的): 公司:

<div id="dCompanyInfo">
 <div>
  <div>
  <%=Html.LocalizableLabelFor(company => company.Name, Register.CompanyName) %>
  </div>
  <div>
  <%=Html.TextBoxFor(company => company.Name) %>
  <%=Html.ValidationMessageFor(company => company.Name) %>
  </div>
 </div>
 <div>
  <div>
  <%=Html.LocalizableLabelFor(company => company.Phone, Register.Phone) %>
  </div>
  <div>
  <%=Html.TextBoxFor(company => company.Phone) %>
  <%=Html.ValidationMessageFor(company => company.Phone) %>
  </div>
 </div>
 <div>
  <div>
  <%=Html.LocalizableLabelFor(company => company.Fax, Register.Fax) %>
  </div>
  <div>
  <%=Html.TextBoxFor(company => company.Fax) %>
  <%=Html.ValidationMessageFor(company => company.Fax) %>
  </div>
 </div>
 <div>
  <div>
  <%=Html.LocalizableLabelFor(company => company.Size_ID, Register.CompanySize) %>
  </div>
  <div>
  <%=Html.ValueListDropDown(company => company.Size_ID, (CodeRoad.AQua.DomainModel.ValueList)ViewData["CompSize"], (string)ViewData["Culture"]) %>
  <%=Html.ValidationMessageFor(company => company.Size_ID) %>
  </div>
 </div>
 <div>
  <div>
  <%=Html.LocalizableLabelFor(company => company.Industry_ID, Register.Industry) %>
  </div>
  <div>
  <%=Html.ValueListDropDown(company => company.Industry_ID, (CodeRoad.AQua.DomainModel.ValueList)ViewData["Industry"], (string)ViewData["Culture"]) %>
  <%=Html.ValidationMessageFor(company => company.Industry_ID) %>
  </div>
 </div>
</div>

对于客户

<div id="dCustomerInfo"> 
      <div>
       <div>
        <%=Html.LocalizableLabelFor(customer => customer.Email, Register.Email) %>
       </div>
       <div>
        <%=Html.TextBoxFor(customer => customer.Email) %>
        <%=Html.ValidationMessageFor(customer => customer.Email) %>
       </div>
      </div>
      <div>
       <div>
        <%=Html.LocalizableLabelFor(customer => customer.Male, Register.Gender) %>
       </div>
       <div>
        <%=Html.ListBoolEditor(customer => customer.Male, Register.Male, Register.Female, Register.GenderOptionLabel) %>
        <%=Html.ValidationMessageFor(customer => customer.Male) %>
       </div>
      </div>
      <div>
       <div>
        <%=Html.LocalizableLabelFor(customer => customer.FirstName, Register.FirstName) %>
        </div>
        <div>
        <%=Html.TextBoxFor(customer => customer.FirstName) %>
        <%=Html.ValidationMessageFor(customer => customer.FirstName) %>
        </div>
      </div>
      <div>
        <div>
        <%=Html.LocalizableLabelFor(customer => customer.LastName, Register.LastName) %>
        </div>
        <div>
        <%=Html.TextBoxFor(customer => customer.LastName) %>
        <%=Html.ValidationMessageFor(customer => customer.LastName) %>
        </div>
      </div>
      <div>
        <div>
        <%=Html.LocalizableLabelFor(customer => customer.Role_ID, Register.Role) %>
        </div>
        <div>
        <%=Html.ValueListDropDown(customer => customer.Role_ID, (CodeRoad.AQua.DomainModel.ValueList)ViewData["OrgRole"], (string)ViewData["Culture"])  %>
        <%=Html.ValidationMessageFor(customer => customer.Role_ID) %>
        </div>
      </div>   
 </div>

有一些自制的扩展方法,但它们在以前的版本(VS RC)中运行良好。生成的Html也没问题,没有“Company.Phone”式的东西。所以我想知道,所有这些“公司”的钥匙来自哪里,我该怎么办呢?我很感激任何解决方案。

1 个答案:

答案 0 :(得分:0)

我怀疑模型绑定方面有一些变化,特别是模板方面的变化。我的猜测是,在模型绑定期间,“Company.Phone”元素被添加到模型状态,因为您用于表单操作的模型包含具有子属性(如Phone)的Company属性。遗憾的是,您的HTML不是以模型绑定器所期望的方式生成的 - 它不知道属性Phone真的是模型上Company属性的子属性。我建议重构您的视图,以便为HTML生成使用模板而不是部分视图。构建模板引擎是为了理解模型层次结构,并在HTML中生成正确的前缀属性,以便模型绑定器可以使用返回的表单值。

布拉德·威尔逊有一系列很好的articles on templating可能会有所帮助。