如何将文本框中输入的值传递给 PartialViewResult?

Posted

技术标签:

【中文标题】如何将文本框中输入的值传递给 PartialViewResult?【英文标题】:How can I pass a value entered in a texbox to a PartialViewResult? 【发布时间】:2013-10-08 04:41:44 【问题描述】:

我正在创建一个 MVC 应用程序,用户可以在其中将商品添加到他们的购物车。他们还可以对某些项目进行部分付款,因此我有一个文本框供他们指定他们想要支付的金额。我正在使用 Ajax ActionLink 来处理更新/添加到购物车操作,这样我就可以在不使用局部视图刷新屏幕的情况下增加购物车计数。我的问题是我找不到将 TextBox 中输入的值传入或访问到我的 PartialViewResult 函数的方法。

这是我的模型...

Public Class StudentSchoolFee_Transaction

    Public Property SchoolFeeId As Integer
    Public Property Title As String
    Public Property Price As Decimal
    Public Property AmountDue As Decimal
    <DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="0:C2")>
    Public Property Amount As Decimal
    Public Property Description As String
    Public Property AcceptPartialPayment As Boolean
    Public Property StudentId As Integer

    Public Property TransactionId As Integer

End Class

Public Class AssignedFeesModel

    Public Property StudentId As Integer
    Public Property StudentNumber As Long
    Public Property SiteId As String

    Public Property SelectedSchoolFeeId As Integer
    Public Property SelectedAcceptPartial As Boolean
    Public Property SelectedAmountDue As Decimal
    Public Property SelectedAmount As Decimal
    Public Property SelectedTransactionId As Integer

    Public Property AssignedFeesCol As System.Collections.Generic.List(Of   StudentSchoolFee_Transaction)

    Public Sub New()

    End Sub

    Public Sub New(ByVal _Deliver As EMS.Grid.Deliver, ByVal _StudentId As String)

        Dim SelectedStudent As New Library.Student(_Deliver, _StudentId)

        AssignedFeesCol = New System.Collections.Generic.List(Of StudentSchoolFee_Transaction)

        StudentId = SelectedStudent.Id
        StudentNumber = SelectedStudent.StudentNumber
        SiteId = SelectedStudent.SiteId

        'Load AssignedFeesCol   
    End Sub
End Class

这是我的初始加载 ActionResult 和我的 AddAssignedFee PartialViewResult 以刷新购物车计数...

    Function AssignedFees(ByVal StudentId As String, Optional ByVal ErrorMessage As String = "") As ActionResult
        Dim oDeliver As New EMS.Grid.Deliver
        oDeliver.UDLNameOrConnString = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString

        Dim m As New AssignedFeesModel(oDeliver, StudentId)

        Dim stu As New Library.MealHistoryDB.Student(oDeliver, m.StudentNumber, UserSession.GetSession.DistrictId)

        Return View(m)
    End Function

    Public Function AddAssignedFee(ByVal StudentId As Integer, ByVal SchoolFeeId As Integer, ByVal SelectedAmount As Decimal) As PartialViewResult

        Dim oDeliver As New EMS.Grid.Deliver
        oDeliver.UDLNameOrConnString = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString

        With New Library.Ecommerce.SchoolFee(oDeliver, SchoolFeeId)
            .AddToCart(oDeliver, UserSession.GetSession.ParentId, StudentId, SelectedAmount)
        End With

        Return PartialView("_CartButton") ', New Global.MSM.mobile.CartButton())

    End Function

这是我的 Ajax 操作链接,第一个是用于添加未指定金额的项目,它可以工作。第二个是更新一个可以有部分付款金额的项目,我找不到将金额传递给 PartialViewResult 的方法。

@Ajax.ActionLink("Add", "AddAssignedFee", "Parent", New With .StudentId = currentItem.StudentId, .SchoolFeeId = currentItem.SchoolFeeId, .SelectedAmount = currentItem.Amount, New AjaxOptions() With .HttpMethod = "POST", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "btnCartContainer", New With .class = "button")


@Ajax.ActionLink("Update", "AddAssignedFee", "Parent", New With .StudentId = currentItem.StudentId, .SchoolFeeId = currentItem.SchoolFeeId, .SelectedAmount = currentItem.Amount, New AjaxOptions() With .HttpMethod = "POST", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "btnCartContainer", New With .class = "button")

我也为更新链接尝试了“.SelectedAmount = Model.SelectedAmount”,但我似乎找不到将输入的金额传递给 PartialViewResult 的方法。

有什么建议吗?

谢谢! 林赛

【问题讨论】:

【参考方案1】:

您可以尝试进行 ajax 调用

$('.Link').on('click', function()
    $.ajax(
        url: "@(Url.Action("AddAssignedFee", "Controller")",
        type: "POST",
        data:  textValue: $('.PaymentAmount').val(), data2: 'data2' 
        cache: false,
        async: true,
        success: function (result) 
            $(".Content").html(result);
        
    );
);

希望这会有所帮助

【讨论】:

以上是关于如何将文本框中输入的值传递给 PartialViewResult?的主要内容,如果未能解决你的问题,请参考以下文章

java将文本框中的数据怎么传到程序里

如何让组合框将正确的值传递给表?

如何从 Javascript 提示框中获取值并将其传递给 PHP 变量以便能够保存在 SQL 中?

C#做winform时,登录系统后怎么写判断一个子窗体的文本框中输入的密码是不是与当前登录的密码一致?

Access表单上的文本框传递先前的值

如何获取HTML中用户输入到文本框中的内容?