返回控制器进行发布时未设置模型属性

发布于 2024-12-02 23:23:35 字数 2871 浏览 1 评论 0原文

看着这个我都要瞎了当我在视图上单击“保存”时,发送到控制器的保存方法的模型没有设置任何值,它们都是默认值。 我缺少什么?我实际上正在使用另一个项目中完全相同的技术...... 数据在我的视图中填充得很好,我可以编辑它并单击保存,代码进入控制器中的 SetSiteTerms 方法,但没有视图中的任何数据。 :-( 我知道这是简单而愚蠢的事情,但我只是还看不到它。

模型:

Imports System.ComponentModel.DataAnnotations
Imports System.Runtime.Serialization

<KnownType(GetType(SiteToA))> _
Public Class SiteToA

    <ScaffoldColumn(False)> _
    Public Property ID As Integer = 0
    Public Property AgreementHTML As String = String.Empty
    Public Property AgreementName As String = String.Empty
    Public Property IsActive As Boolean = False

    Public Sub New()
        MyBase.New()
    End Sub
End Class

视图:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of Community_Portal_Admin.SiteToA)" %>
<% Using Html.BeginForm("SetSiteTerms", "SiteToA", FormMethod.Post, New With {.id = "SiteTermsForm"})%>


    <div class="gridFrame">
        <% Html.Telerik().EditorFor(Function(m) m.AgreementHTML) _
                    .Name("AgreementHTML") _ ' Name must be the same as the property name in the model
                    .Encode(True) _
                    .HtmlAttributes(New With {.style = "height:300px;"}) _
                    .Render()%>
        <br />
        <div class="smallFrameLeft">        
            <%: Html.ActionLink("Cancel", "Index", "Home", Nothing, New With {.class = "t-button", .Style = "font-size:12px;"})%>
        </div>
        <div class="smallFrameRight">
            <input type="submit" value="Save" class="t-button" />       
        </div>
    </div>
<% End Using%>

控制器:

Imports Telerik.Web.Mvc

Public Class SiteToAController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        Dim setting As SiteToA
        Try
            setting = SiteToARepository.One(Function(d) d.IsActive = True)
            ViewData.Model = setting
            Return PartialView()
        Catch ex As Exception
            Throw
        End Try
    End Function

    <HttpPost()> _
    Function SetSiteTerms(ByVal model As SiteToA) As ActionResult
        Dim setting As SiteToA
        Try
            setting = SiteToARepository.One(Function(d) d.ID = model.ID)
            TryUpdateModel(setting)
            If Not SiteToARepository.Update(setting) Then Throw New Exception("There was a problem during update")
            Return PartialView()
        Catch ex As Exception
            Return PartialView()
        Finally
            If Not setting Is Nothing Then
                setting.Dispose()
                setting = Nothing
            End If
        End Try
    End Function
End Class

编辑: 我已经更新了我的视图和控制器。为什么填充我的模型的任何想法都可以在视图中进行编辑,但是当我使用保存按钮发布时,发送到控制器的模型没有数据???

I'm going blind staring at this. When I click save on the view, the model being sent to the save method of the controller has no values set, they're all default values.
What am I missing? I'm actually using the exact same technique from another project...
The data is populated just fine in my view, I can edit it and click save, the code enters the SetSiteTerms method in the controller but has none of the data from the view. :-(
I know this is something simple and dumb but I just can't see it yet.

Model:

Imports System.ComponentModel.DataAnnotations
Imports System.Runtime.Serialization

<KnownType(GetType(SiteToA))> _
Public Class SiteToA

    <ScaffoldColumn(False)> _
    Public Property ID As Integer = 0
    Public Property AgreementHTML As String = String.Empty
    Public Property AgreementName As String = String.Empty
    Public Property IsActive As Boolean = False

    Public Sub New()
        MyBase.New()
    End Sub
End Class

View:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of Community_Portal_Admin.SiteToA)" %>
<% Using Html.BeginForm("SetSiteTerms", "SiteToA", FormMethod.Post, New With {.id = "SiteTermsForm"})%>


    <div class="gridFrame">
        <% Html.Telerik().EditorFor(Function(m) m.AgreementHTML) _
                    .Name("AgreementHTML") _ ' Name must be the same as the property name in the model
                    .Encode(True) _
                    .HtmlAttributes(New With {.style = "height:300px;"}) _
                    .Render()%>
        <br />
        <div class="smallFrameLeft">        
            <%: Html.ActionLink("Cancel", "Index", "Home", Nothing, New With {.class = "t-button", .Style = "font-size:12px;"})%>
        </div>
        <div class="smallFrameRight">
            <input type="submit" value="Save" class="t-button" />       
        </div>
    </div>
<% End Using%>

Controller:

Imports Telerik.Web.Mvc

Public Class SiteToAController
    Inherits System.Web.Mvc.Controller

    Function Index() As ActionResult
        Dim setting As SiteToA
        Try
            setting = SiteToARepository.One(Function(d) d.IsActive = True)
            ViewData.Model = setting
            Return PartialView()
        Catch ex As Exception
            Throw
        End Try
    End Function

    <HttpPost()> _
    Function SetSiteTerms(ByVal model As SiteToA) As ActionResult
        Dim setting As SiteToA
        Try
            setting = SiteToARepository.One(Function(d) d.ID = model.ID)
            TryUpdateModel(setting)
            If Not SiteToARepository.Update(setting) Then Throw New Exception("There was a problem during update")
            Return PartialView()
        Catch ex As Exception
            Return PartialView()
        Finally
            If Not setting Is Nothing Then
                setting.Dispose()
                setting = Nothing
            End If
        End Try
    End Function
End Class

EDIT:
I've updated my view and controller. Any ideas why my model is populated, can be editing in the view but when I post with the save button, the model sent to the controller has no data???

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

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

发布评论

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

评论(1

动次打次papapa 2024-12-09 23:23:35

我已经更新了原始帖子以显示 Telerik 提供的答案,而且我对模型其余部分的问题是一个愚蠢的缺失:哈哈

I've updated the orginal post to show the answer provided by Telerik, also my problem with the rest of the model was a silly missing : lol

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