使用.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) %>
凡在你看來,你想渲染兩個不同的地址?我只能看到一個與你的主視圖模型相關的「RenderPartial」......也許你沒有解釋清楚,因爲我不明白這裏似乎是什麼問題 – 2010-11-21 09:19:59
我嘗試添加行:<%Html.RenderPartial(「AddressEditPartial」,this.Model);%>兩次。這就是我試圖添加2個不同的地址,但是這不正確。 – Mike 2010-11-22 14:11:11