使用.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>
这是局部视图:
<%@ 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 :(得分:6)
错误:
<% Html.RenderPartial("Name", model ) %>
<% Html.RenderPartial("Name", differentModel ) %>
答案 1 :(得分:1)
我不完全确定你需要什么。但是,如果我理解正确,您的模型中会有List<Address>
类型的属性。如果问题似乎是从客户端到服务器获取此类数据,请阅读this blog post以帮助您解决问题。
如果问题是在父视图中显示不同的地址,那只是一个简单的foreach
语句:
<% foreach(Address a in this.Model.Addresses) %>
<% { %>
<% Html.RenderPartial("Address", a); %>
<% } %>
答案 2 :(得分:1)
我可能不清楚你在寻找什么,但基本上你的情况是你有一个用户列表,每个用户都有一个地址列表。因此,您将遍历您的用户列表,并为每个用户迭代他们的地址列表(此代码段是嵌入在aspx页面中的c sharp示例:
<% 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
<% } %>
<% } %>