View with a lot of Components. Multiple Model Objects?
I am developing a little MVC app.
The problem: I have a view page which has a form to add events, a form to search for events and a list of events.
It's not a big deal to place the components in single views but I need them in one page. I want a permanent form above the list of events.
The problem is that I don't know what to choice as a model object for the view. If I choice a List of events it's working perfectly with the List but not with the form. If I choice a specific Model Object fitting to the form the list hast problem.
What's the best way to handle such views with forms, lists,... ?
That's my View:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="ViewPage<EventPlaza.Web.Models.EventsListViewModel>" %>
<%@ Import Namespace="EventPlaza.Storage.Model" %>
<%@ Import Namespace="EventPlaza.Web.HtmlHelpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="EventControlsContainer">
<h3><a href="#">Add Event Link</a></h3>
<div>
<% H开发者_开发知识库tml.RenderPartial("AddEvent", new AddEventModel()); %>
</div>
<h3><a href="#">Search for Events</a></h3>
<div>
<form>
Place:
<input id="elementID" name="elementID" type="text" class="inputSearch" />
Date:
<input id="datepicker" name="elementID2" type="text" class="inputSearch" />
<input id="addEvent" type="submit" value="Find Events" />
</form>
</div>
</div>
<% foreach(var product in Model.Events) { %>
<% Html.RenderPartial("EventSummary", product); %>
<% } %>
<div class="pager">
<%: Html.PageLinks(Model.PagingInfo,
x => Url.Action("List", new {page=x})) %>
</div>
</asp:Content>
This the AddEvent View Control:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EventPlaza.Storage.Model.AddEventModel>"%>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("AddEvent", "Event", FormMethod.Post)) { %>
Event Link:
<%: Html.TextBoxFor(x => x.EventLink) %>
Event Name:
<%: Html.TextBoxFor(x => x.EventName) %><br />
Place:
<%: Html.TextBoxFor(x => x.Place) %>
Starting Date:
<%: Html.TextBoxFor(x => x.StartingDate) %> <br />
End Date:
<%: Html.TextBoxFor(x => x.EndDate) %>
<input id="Submit" type="submit" value="Add Event" />
<% } %>
I strongly recommend that you should not be using the Model object from the your Domain Model directly as the type of view instead you should make a custom EventViewModel and keep three different objects corresponding to each partial view you have.
精彩评论