查找在网页上多次使用相同局部视图的示例

Mik*_*ike 2 asp.net-mvc

使用.NET MVC,我有一个部分视图,我需要在同一页面上多次实现一个地址,因为用户可能有不同类型的地址.作为.NET MVC的新手,我想找一个能够指导我完成所有必要步骤的例子.

这是一些代码.代码的第一部分是包含局部视图的主页面.我知道这是错误的,但我首先尝试两次列出"Html.RenderPartial"行(针对2个不同的地址).我这样做只是为了看它会做什么.

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

这是局部视图:

<%@ 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) %>  
Run Code Online (Sandbox Code Playgroud)

Joh*_*ell 6

什么错了:

<% Html.RenderPartial("Name", model ) %>
<% Html.RenderPartial("Name", differentModel ) %>
Run Code Online (Sandbox Code Playgroud)