gpt4 book ai didi

c# - 尝试使 View 模型项等于变量时出现空值错误

转载 作者:太空狗 更新时间:2023-10-29 20:36:18 25 4
gpt4 key购买 nike

我正在创建一个 MVC 应用程序,并在其中向 JIRA 发送电子邮件。当我在仅 IssueTable 的 View 中使用 ModelType 时,我最初让它工作,但是当我也更改它时 ModelType ViewModelClass.ViewModel 它停止正常工作。

在 Controller 中:

Public Function SubmitIssue(issuetable As IssueTable, test As IssueTracker.ClientUserProjectIssue ) As ActionResult

Dim mail As New MailMessage()
mail.Subject = issuetable.IssueSummary
Dim body As String = test.iTable.IssueDescription
mail.Body = body
smtp.Send(mail)

在 View 中:

@ModelType IssueTable

@Html.EditorFor(Function(model) model.IssueSummary)
@Html.EditorFor(Function(model) model.IssueDescription)

这最初有效,但我需要有多个表来发送数据,所以我创建了一个像这样的 viewModel:

Public Class ViewModel
Public proTableList As List(Of ProjectType)
Public cTableList As List(Of ClientTable)
Public proTable As ProjectType
Public iTable As IssueTableViewModel
End Class

Public Class IssueTableViewModel
Public IssueSummary As String
Public IssueDescription As String
End Class

在 View 中我显示了这样的信息:

@ModelType IssueTracker.ViewModel

@Html.EditorFor(Function(model) model.iTable.IssueSummary)
@Html.EditorFor(Function(model) model.iTable.IssueDescription)

现在问题出在我的 Controller 中,因为我尝试像这样填充描述和摘要的方式出现空错误:

Public Function SubmitIssue(issuetable As IssueTable, test As IssueTracker.ClientUserProjectIssue ) As ActionResult

Dim mail As New MailMessage()
mail.Subject = test.iTable.IssueSummary
Dim body As String = test.iTable.IssueDescription
mail.Body = body
smtp.Send(mail)

正如您所看到的,除了添加到代码中的明显 ViewModel 类之外,它完全相同。

这显然不是使用 viewModel 将值放入变量/电子邮件的正确方法,有人知道我如何正确地做到这一点吗?

最佳答案

您需要删除 BindAttribute从你的 Controller 方法,或将其更改为 <Bind(Include:="iTable")>

您的 View 模型不包含名为 IssueID 的属性, IssueSummaryIssueDescription , 只有一个名为 iTable 的属性(它又包含这些属性)。

但是您使用的是 View 模型,而 View 模型应该只包含您在 View 中需要的属性,这意味着您不需要BindAttribute。在你的方法上。

假设您只编辑 IssueSummaryIssueDescription IssueTable 的属性(根据您的代码),那么您的 View 模型应该是

Public Class ViewModel
Public proTableList As List(Of ProjectType)
Public cTableList As List(Of ClientTable)
Public proTable As ProjectType
// Public iTable As IssueTable // delete this
Public IssueSummary As String // add this
Public IssueDescription As String // add this
End Class

关于c# - 尝试使 View 模型项等于变量时出现空值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com