2010-05-29 70 views
1

最近我安裝了VS 2010 Release(從RC遷移),我的MVC應用程序不再工作。更具體一點:我有一個嚮導,其中包含幾個步驟來創建新的客戶帳戶(Jquery表單嚮導,但它並不重要)。每個步驟都包含一個鍵入的部分視圖,用於賬戶的每個部分:公司,客戶,許可等。提交表單時,我在ModelState中看到了很奇怪的事情。公司有重複密鑰:帶「公司」前綴,沒有。事情是這樣的:ASP.NET MVC 2奇怪的行爲

[6]「Company.Phone」串
[12]「手機」串

所有這些鍵我的模式狀態是無效的,因爲公司實際上是零和驗證失敗。當它是RC時,沒有帶「公司」前綴的這樣的鍵。因此,在安裝VS Release之後,出現帶有前綴「Company」的ModelState中的這些鍵。這裏是我的代碼: 主視圖

<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」類似的東西。所以我想知道,所有與「公司」有關的鑰匙是從哪裏來的,我該怎麼辦?我欣賞任何解決方案。

回答

0

我懷疑在模型綁定方面有一些變化,特別是在模板方面。我的猜測是,在模型綁定期間,「Company.Phone」元素被添加到模型狀態,因爲您用於表單操作的模型包含具有子屬性(例如Phone)的Company屬性。不幸的是,您的HTML並非像模型聯編程序所期望的那樣生成 - 它不知道該屬性Phone實際上是模型上Company屬性的子屬性。我建議重構你的視圖來爲你的HTML代使用模板而不是部分視圖。模板引擎用於理解模型層次結構,並將在HTML中生成正確的前綴屬性,以便模型綁定器可以使用返回的表單值。

Brad Wilson有一個不錯的articles on templating系列,它可能會有幫助。