views:

221

answers:

2

Hello hello. Here's what I have. I have an MVC application where all the data is tied together by VisitDate table in my database. The home page is a strongly typed view of type VisitDate, all it does is pull up some simple data. Now, here's where I'm having a problemo. I need a link that passes the current model in the view back to a seperate controller action so I can render a different page with different data.

Here are my two controller actions. I'm going from News.aspx to FrontPage.aspx and hopefully passing SchoolVisit.

Function News(ByVal SchoolVisit As SchoolVisitDate) As ActionResult

    Dim db As New NewsData.NewsDB
    Dim repos As New NewsRepository

    Dim _classId As Integer
    _classId = (From a In db.SchoolClasses Where a.VisitDateID = SchoolVisit.VisitDateID Select a.ClassID).Single()

    ViewData("VisitDate") = FormatDateTime(SchoolVisit.VisitDate, vbShortDate)

    ViewData("Staff") = repos.GetStaff(_classId)
    ViewData("StockArticles") = From a In db.StockArticles Select a

    ViewData("Articles") = repos.GetArticles(_classId)

    Return View()
End Function

Function FrontPage(ByVal SchoolVisit As SchoolVisitDate) As ActionResult

    Dim repos As New NewsRepository
    Dim _VisitDateID As Integer

    _VisitDateID = SchoolVisit.VisitDateID

    ViewData("Editorial") = repos.GetEditorial(_VisitDateID)
    Return View()
End Function
+1  A: 

Html.ActionLink can be of help

ajay_whiz
I've been messing with that and can't seem to get the syntax right.<%: Html.ActionLink("Page", "FrontPage", "Home", New With {.VisitDate = Model})%>This passes back a route value of ?Length=4
keynone
you will have to pass model data as named parameter e.g. <%: Html.ActionLink("Page", "FrontPage", "Home", visitDate=Model.VisitDate})%>
ajay_whiz
A: 

Here's what you have to do:

  1. You should make the News view strongly typed for SchoolVisitDate.
  2. Have a form that submits the strongly typed SchoolVisitDate stuff post back to controller
  3. Add a Post method to your controller
  4. Have that post method redirect to the FrontPage view.
Shlomo
This would work, however, the link is in the menu and I don't feel like fudging css to make the submit button look like a text link :/
keynone
I'm guessing you're coming over from WebForms... MVC is very helpful if you buy into it. Your code looks Webforms-esque and you won't really leverage the power of it this way.
Shlomo
You want to use Strongly typed models the vast majority of the time, http-post when you need to send stuff back allowing user feedback, http-get with hidden id references when if you don't.
Shlomo