繁体   English   中英

ASP.NET MVC 2奇怪的行为

[英]ASP.NET MVC 2 strange behavior

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

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

我的所有这些键的模型状态均无效,因为Company实际上为null且验证失败。 在RC时,没有这样的带有“ Company”前缀的键。 因此,在安装了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”之类的东西。 所以我想知道,“公司”的所有这些密钥来自哪里,我该怎么办? 我感谢任何解决方案。

我怀疑在模型绑定方面,特别是在模板方面,存在一些更改。 我的猜测是,在模型绑定期间,“ Company.Phone”元素将添加到模型状态,因为用于表单操作的模型包含具有子属性(例如Phone)的Company属性。 不幸的是,您的HTML并不是按照模型绑定程序的预期生成的-它不知道Phone属性实际上是该模型上Company属性的子属性。 我建议重构您的视图以使用模板而不是部分视图来生成HTML。 建立模板引擎是为了了解模型层次结构,并将在HTML中生成正确的前缀属性,以便模型绑定程序可以使用返回的表单值。

布拉德·威尔逊(Brad Wilson) 在模板方面有一系列不错的文章 ,可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM