【问题标题】:Model properties not set when returning to the controller for post返回控制器进行发布时未设置模型属性
【发布时间】:2011-09-03 19:28:12
【问题描述】:

我盯着这个要瞎了。当我在视图上单击保存时,发送到控制器保存方法的模型没有设置任何值,它们都是默认值。 我错过了什么?我实际上正在使用来自另一个项目的完全相同的技术...... 数据在我的视图中填充得很好,我可以编辑它并单击保存,代码进入控制器中的 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

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

【问题讨论】:

  • 没有帮助吗?它必须是一些简单的东西。在这里绞尽脑汁......

标签: vb.net asp.net-mvc-3 vb.net-2010 telerik-editor


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2011-11-12
    • 2013-08-30
    相关资源
    最近更新 更多