0

How i can do view for such code, if i have join? in asp.net mvc on c# language

 public ActionResult Details(int id)
        {
            var routeDetails = (from rd in db.Route 
                                join rdd in db.RouteDetail
                                on rd.RouteId equals rdd.RouteId 
                                where rd.RouteId == id 
                                select new
                                        {
                                            RouteId = rd.RouteId,
                                            Name = rd.Name,
                                            Station = rdd.Station,
                                            TimeArrival = rdd.TimeArrival,
                                            TimeDeparture = rdd.TimeDeparture
                                        }).First();
            return View(routeDetails);
        }

I can do view for one table without join, but how it will be with join? here is how i do it without join for edit action:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<TrainShedule.Models.Route>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Edit</h2>
    <% using (Html.BeginForm())
       {%>
    <%= Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fields</legend>
        <ul>
            <li>Название маршрута:
                <%= Html.TextBoxFor(m => m.Name) %>
                <%= Html.ValidationMessageFor(m => m.Name)%>
            </li>
        </ul>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
    <% } %>
    <div>
        <%= Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

1 Answer 1

1

You're creating an anonymous type for your routeDetails variable. If you want a strongly-typed view, you can define a new model class (RouteRouteDetail or similar) that contains the properties you want from both tables, and select instances of that type rather than selecting anonymous type instances using the new {} syntax.

3
  • I didn't understand, could you explain more detaily?
    – DPS
    Nov 21, 2011 at 11:25
  • ok, i do this, thank you. Now i have more one question: how i can view with such include Inherits="System.Web.Mvc.ViewPage<IEnumerable<TrainShedule.Models.RouteDetailsAndRoute>>" foreach - for list, and simple html.encode - for one item
    – DPS
    Nov 21, 2011 at 18:09
  • <% var oneitem = Model.ElementAt(0); %> <%= Html.Encode(oneitem.RouteId)%> i have done this for upper question, but if it is more effective answer please give it'
    – DPS
    Nov 21, 2011 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.