将模型从强类型视图传递回控制器操作 - mvc2 VB.net

发布于 2024-09-13 08:14:19 字数 1114 浏览 6 评论 0原文

这就是我所拥有的。我有一个 MVC 应用程序,其中所有数据都通过数据库中的 VisitDate 表绑定在一起。主页是 VisitDate 类型的强类型视图,它所做的只是提取一些简单的数据。现在,这就是我遇到问题的地方。我需要一个链接,将视图中的当前模型传递回单独的控制器操作,以便我可以使用不同的数据渲染不同的页面。

这是我的两个控制器操作。我将从 News.aspx 转到 FrontPage.aspx,并希望能够通过 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2024-09-20 08:14:19

Html.ActionLink 可以提供帮助

Html.ActionLink can be of help

别靠近我心 2024-09-20 08:14:19

您需要执行以下操作:

  1. 您应该将 News 视图设为 SchoolVisitDate 的强类型。
  2. 拥有一个将强类型 SchoolVisitDate 内容提交回控制器的表单
  3. 将 Post 方法添加到控制器
  4. 将该 post 方法重定向到 FrontPage 视图。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文