2010-11-19 31 views
2

使用.NET MVC,我有一個地址的局部視圖,我需要在同一頁面上多次實現,因爲用戶可能有不同類型的地址。作爲.NET MVC的新手,我想找一個能夠指導我完成所有必要步驟的示例。尋找在網頁上多次使用相同的局部視圖的例子

這是一些代碼。代碼的第一部分是包含部分視圖的主頁面。我知道這是錯誤的做法,但我首先嚐試了兩次(2個不同的地址)列出「Html.RenderPartial」行。我只是爲了看看它會做什麼。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<JCIMS_MVC2_EF.DomainModel.Data.Models.DistrictAddressModel>" %> 


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Create District 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<% Html.RenderPartial("DistrictHeaderPartial", this.ViewData.Model.DistrictHeader); %> 

<% Html.EnableClientValidation(); %> 

<% using (Html.BeginForm()) { %> 

    <fieldset id="address"> 
    <legend>Create new Address for <%: Model.DistrictHeader.District_Name %></legend> 

     <% Html.RenderPartial("AddressEditPartial", this.Model); %> 

    </fieldset> 
    <p> 
    <input type="submit" value="Save" /> 
    </p> 


<% } %> 

<div> 
    <%: Html.ActionLink("Return to District Info Page", "Index", "District", new { id = Model.DistrictHeader.District_GUID }, null)%> 
</div> 

</asp:Content> 

這是局部視圖:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<JCIMS_MVC2_EF.DomainModel.Data.Models.BaseAddressModel>" %> 

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     $("#AddressViewData_Zip_Code").mask("99999"); 
    }); 
</script> 

<div class="editor-label"> 
<%: Html.LabelFor(model => model.AddressViewData.Address_Type_Text) %>  
</div> 
<%: Html.DropDownList("AddressViewData.Address_Type_Code")%> 
<%: Html.ValidationMessageFor(model => model.AddressViewData.Address_Type_Code) %> 
<div class="editor-label"> 
<%: Html.LabelFor(model => model.AddressViewData.Street_Address)%> 
</div>       
<%: Html.TextBoxFor(model => model.AddressViewData.Street_Address, new { @class = "input-size" })%> 
<%: Html.ValidationMessageFor(model => model.AddressViewData.Street_Address) %> 
<div class="editor-label"> 
<%: Html.Label("City") %> 
</div> 
<%: Html.TextBoxFor(model => model.AddressViewData.City, new { @class = "input-size" })%> 
<div class="editor-label"> 
<%: Html.Label("State") %> 
</div> 
<%: Html.DropDownList("AddressViewData.State")%> 
<div class="editor-label"> 
<%: Html.LabelFor(model => model.AddressViewData.Zip_Code) %> 
</div> 
<%: Html.TextBoxFor(model => model.AddressViewData.Zip_Code) %> 
<%: Html.ValidationMessageFor(model => model.AddressViewData.Zip_Code) %> 
<div class="editor-label"> 
<%: Html.LabelFor(model => model.AddressViewData.Address_ATTN) %> 
</div> 
<%: Html.TextBoxFor(model => model.AddressViewData.Address_ATTN, new { @class = "input-size" })%> 
<%: Html.ValidationMessageFor(model => model.AddressViewData.Address_ATTN) %> 
+0

凡在你看來,你想渲染兩個不同的地址?我只能看到一個與你的主視圖模型相關的「RenderPartial」......也許你沒有解釋清楚,因爲我不明白這裏似乎是什麼問題 – 2010-11-21 09:19:59

+0

我嘗試添加行:<%Html.RenderPartial(「AddressEditPartial」,this.Model);%>兩次。這就是我試圖添加2個不同的地址,但是這不正確。 – Mike 2010-11-22 14:11:11

回答

6

哪些錯誤有:

<% Html.RenderPartial("Name", model) %> 
<% Html.RenderPartial("Name", differentModel) %> 
+0

jfar-yup,同意.. +1 – 2010-11-19 22:09:33

1

我不能完全確定你所需要的。但是如果我正確地理解了你,你的模型中有一個List<Address>類型的屬性。如果問題似乎是從客戶端獲取這種數據到服務器,然後閱讀this blog post,這將幫助你。

如果問題出在它只是一個簡單的foreach聲明父視圖顯示不同的地址:

<% foreach(Address a in this.Model.Addresses) %> 
<% { %> 
    <% Html.RenderPartial("Address", a); %> 
<% } %> 
1

我可不清楚你在找什麼,但基本上你的情況是,你有一個用戶列表,每個用戶都有一個地址列表。所以,你會通過你的用戶列表,併爲每個用戶自己的,像這樣的地址列表迭代(此代碼段是嵌入在一個aspx頁面升C的例子:

<% foreach(User lvUser in Model.Users) 
{ %> 
    <% Html.Encode(lvUser.Username) %> 
    <% Html.Encode(lvUser.Email) %> 

    <% foreach(Address lvAddress in lvUser.Addresses) 
    { %> 
     <% Html.RenderPartial("PartialAddress", lvAddress); %> //Where you've set the view data class to Address 
    <% } %> 


<% } %> 
相關問題