从下拉框中选择项目时尝试激活特定的控制器功能

Trying to activate a specific Controller function when an Item is selected from a Dropdown Box

.net 编程新手。我正在使用 Visual Studios 2017,在 vb.net 中编程,使用 MVC 设置(无论那意味着什么),以及使用 Razor 的 html 网页 - 我希望这些信息对你来说足够了。我正在更新别人的代码,所以这不是从头开始写的。我正在尝试在创建屏幕上添加一个动态网格(看起来像一个 excel 电子表格),其中第一个 row/column 项目是一个下拉列表,该行中的其他列是来自数据库的字段 table(sql 服务器 2017 v18)。控制器中的 http:Get Create 函数已经存在,用于显示用户输入内容的字段(网格除外),并且 http:Post Create 存在用于将数据放入数据库。 我想出了如何在屏幕上放置局部视图,以便它显示下拉框和(假设)我想显示的数据库 table 中的字段。我怀疑我的网格是否正确,但我知道每次我 select 从下拉框中选择一个项目时,它会自动转到 http:Post 创建而不是我想要它去的功能这样我就可以从 table 中获取数据来显示我想要的项目。我在整个网络上搜索了一个答案,但我唯一能找到的是网络屏幕或 C#/HTML 编码,只是一个下拉列表或一个按钮;没有人像这个程序那样连接东西。

我已经搜索了 Beginform、Partial、Controller、viewbag、Model、ViewModel、重载...只要你能想到的...我已经查过了。 Microsoft 的文档通过使用定义中定义的词来定义术语,因此这无法帮助我理解如何将对象连接在一起以执行我想要的操作。

我寻找看起来像我正在做的例子,但似乎没有人在做我正在做的事;我发现的所有内容要么讨论了每个单独的部分,要么讨论了单个应用程序软件中的解决方案。

难道这个平台不支持在一个BeginForm中嵌套另一个BeginForm?如果这应该有效......我不知道为什么它不起作用。 ''''''''''''

约会控制器:

<HttpGet()>
Public Function Create(memId As Integer?) As ViewResult

''' HTTP:GET -- Appointment Create - 
    Dim mem As Member = db.Members.Find(memId)
    ViewBag.MemID = memId
    ViewBag.DiagCodes = PopulateDiagCodesDropDownList()
    ViewBag.VADiagCdWebgrid = New DiagnosisCode
    ViewBag.NewAppResult = New MemberAppointmentResult With {.ResultID = db.MemberAppointmentResults.Count + 1}
    Return View()
End Function

        ''' HTTP:POST -- Appointment Create - Save button
        <HttpPost()>
        Public Function Create(memId As Integer, <Bind(Exclude:="model.EventHistory,model.Hospital,model.MemberAppointmentResults,model.MtfReason,model.DiagnosisCode")> model As AddAppointmentViewModel, examType As Integer, Optional button As String = Nothing) As ActionResult
            ViewBag.MemID = memId
            ViewBag.DiagCodes = PopulateDiagCodesDropDownList()
            Dim ev As New EventHistory With {
                .MemberID = memId,
                .EventDate = model.ScheduledVisitDate,
                .EventLoggedBy = GetCurrentUserId(),
                .EventSubcodeID = examType
            }
            Dim newAppt = New MemberAppointment
            With newAppt
                .EventHistory = ev
                .MemberEventID = db.EventHistories.Count + 1
                .ScheduledVisitDate = model.ScheduledVisitDate
                .ScheduledVisitTime = model.ScheduledVisitTime
                .HospitalID = model.HospitalID
                .ActualVisitDate = model.ActualVisitDate
                .ActualVisitTime = model.ActualVisitTime
                .IsVisitCompleted = model.IsVisitCompleted
                .CurrentDisabilityPercentage = model.CurrentDisabilityPercentage
                .MtfReasonCodeID = model.MtfReasonCodeID
            End With

            If ModelState.IsValid Then
                db.MemberAppointments.Add(newAppt)
                db.SaveChanges()

                Dim apptID As Integer = newAppt.AppointmentID
**---this section is where the grid would be looped through to add the data to the table (i haven't gotten this far yet)**
                Return RedirectToAction("Index", New With {.id = memId})
            End If
            Return View(model)
        End Function

**---this function is the one I want the dropdown to go to when an Item is selected**

''' HTTP:Post -- Appointment Create. - Add Diagnosis code to the screen before saving the new appointment data
<HttpPost()>
Public Function AddDiagCodeResult(DiagCodes As Integer, <Bind(Include:="ResultID")> result As MemberAppointmentResult) As ActionResult
    'pass in the Diagcodes value that was selected and create a memory place holder that looks like the MemberAppointmentResults table
    'make sure that the DiagCodes variable contains a value
    If DiagCodes = 0 OrElse DiagCodes = Nothing Then
        Return View() 'just go back to the view you were just on
    End If
   'Grab the diagnostic code record
    Dim DiagCodeModel As IEnumerable(Of DiagnosisCode) = From s In db.DiagnosisCodes
                                                         Where s.VADiagnosisCode = DiagCodes
                                                         Order By s.VADiagnosticCodeID
                                                         Select s

    ViewBag.VADiagCdWebgrid = DiagCodeModel.FirstOrDefault
    'find the record in MemberAppointmentResults associated with the selected Diagnosis Code
    Dim VADiag = ViewBag.VADiagCdWebgrid.Find(DiagCodes)
    'Create a boolean to identify that the selected Diagnosis code is in the memberappointmentresults table
    Dim diagCodeExists As Boolean = False
    For Each VADiagCode As MemberAppointmentResult In VADiag.MemberAppointmentResults
        'Identify that the selecte Diagnosis codes is already in the viewmodel
        If VADiagCode.DiagnosisCode.VADiagnosisCode = DiagCodes Then
            diagCodeExists = True
        End If
    Next
    'lets not add duplicate values if we can keep from it.
    If diagCodeExists = False Then
        'determine how many rows already exist and add another one
        result.ResultID = VADiag.VADiagCdWebgrid.Count + 1
        'save the selected Diagnosis Code
        'result.VADiagnosticCodeID = DiagCodeModel.DiagnosticCode
        'get the diagnosis code id from the diagnosis code table
        result.VADiagnosticCodeID = db.DiagnosisCodes.Find(DiagCodes).VADiagnosticCodeID
        'get the diagnosis code description from the diagnosis code table
        'result.DiagnosticCodeDescription = db.DiagnosisCodes.Find(DiagCodes).DiagnosisCodeDescription
        'if that worked, add the new row to the viewmodel
        If ModelState.IsValid Then
            VADiag.Add(result)
        End If
    End If
    ViewBag.NewAppResult = New MemberAppointmentResult With {.ResultID = db.MemberAppointmentResults.Count + 1}
    Return View("Create", VADiag)
End Function

Private Function PopulateDiagCodesDropDownList() As SelectList
    Dim diagCodesQuery = db.DiagnosisCodes.OrderBy(Function(d) d.VADiagnosisCode).Select(Function(d) New With {d.VADiagnosticCodeID, .VADiagnosisCode = d.VADiagnosisCode & " - " & d.DiagnosisCodeDescription}).ToList()
    Return New SelectList(diagCodesQuery, "VADiagnosticCodeID", "VADiagnosisCode")
End Function

Create.vbhtml

@Imports System.Web.Mvc.Html
@Imports PtdrWeb.CompleteValidationAndFormatting
@ModelType PtdrWeb.AddAppointmentViewModel
@Code
    ViewData("Title") = "Create Member Appointment"
End Code
<br>
<div Class="row">
    <div Class="three columns">
        <strong>Create Member Appointment</strong>
    </div>
</div>
<br>
<hr />
@Using Html.BeginForm("Create", "Appointment", New With {.memId = ViewBag.MemID}, FormMethod.Post, New With {.autocomplete = "off"})
    @Html.AntiForgeryToken()
**---all the other fields are listed here... this part actually works**

**---the next line activates a partial view (or screen)**
    @<div Class="row">
        @Html.Partial("AddDiagCdResult", ViewBag.NewAppResult)
    </div>
   @<hr />
End Using

AddDiagCdResult.vbhtml

@ModelType PtdrWeb.MemberAppointmentResult
@Using Html.BeginForm("AddDiagCodeResult", "Appointment")
    @<div class="zebra_stripe_rows">
        <h4>Diagnosis Codes:</h4>
        <hr />
        <div class="row">
            <div class="five columns">
                <strong>Select Diagnostic Codes</strong>
            </div>
            <div class="one column">
                @Html.LabelFor(Function(model) model.ResultID, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="one column">
                @Html.LabelFor(Function(model) model.AppointmentID, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="one column">
                @Html.LabelFor(Function(model) model.VADiagnosticCodeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="one column">
                @Html.LabelFor(Function(model) model.DiagnosisCode.VADiagnosticCodeID, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="two columns">
                @Html.LabelFor(Function(model) model.DiagnosisCode.VADiagnosisCode, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="two columns">
                @Html.LabelFor(Function(model) model.DiagnosisCode.DiagnosisCodeDescription, htmlAttributes:=New With {.class = "control-label col-md-2"})
            </div>
            <div class="two columns">        </div>
        </div>
**---this is where the grid should be displayed**
        <div class="grid-container">
            <div class="five columns">

**---this is the dropdownlist that I want to go to the AddDiagCodeResult function in the Appointment Controller when an Item in the dropdown is selected.**
                @Html.DropDownList("DiagCodes", Nothing, New With {.onchange = "this.form.submit()", .style = "width:575px;"})
            </div>
            <div class="one column">
                @Html.DisplayFor(Function(model) model.ResultID, New With {.htmlAttributes = New With {.class = "grid-item"}})
             </div>
            <div class="one column">
                @Html.DisplayFor(Function(model) model.AppointmentID, New With {.htmlAttributes = New With {.class = "grid-item"}})
            </div>
            <div class="one column">
                @Html.DisplayFor(Function(model) model.VADiagnosticCodeID, New With {.htmlAttributes = New With {.class = "grid-item"}})
            </div>
            <div class="one column">
                @Html.DisplayFor(Function(model) model.DiagnosisCode.VADiagnosticCodeID, New With {.htmlAttributes = New With {.class = "grid-item"}})
            </div>
            <div class="two column">
                @Html.DisplayFor(Function(model) model.DiagnosisCode.VADiagnosisCode, New With {.htmlAttributes = New With {.class = "grid-item"}})
            </div>
            <div class="two columns">
                @Html.DisplayFor(Function(model) model.DiagnosisCode.DiagnosisCodeDescription, New With {.htmlAttributes = New With {.class = "grid-item"}})
            </div>
            <div class="two columns">
                @Html.ActionLink("Remove", "DiagnosisCodeRemove", "Appointment", New With {.id = Model.ResultID}, New With {.class = "secondary button row"})
            </div>
        </div>
    </div>

End Using

AddAppointmentViewModel.vb

Public Class AddAppointmentViewModel
    Public Property AppointmentID As Integer
    Public Property MemberEventID As Integer
    Public Property ScheduledVisitDate As Date
    <VisitTime()>
    Public Property ScheduledVisitTime As TimeSpan?
    Public Property HospitalID As Integer
    Public Property ActualVisitDate As Date?
    <VisitTime()>
    Public Property ActualVisitTime As TimeSpan?
    Public Property ActualHospitalID As Integer?
    Public Property IsVisitCompleted As Boolean
    Public Property CurrentDisabilityPercentage As Byte?
    Public Property MtfReasonCodeID As Integer
    Public Overridable Property EventHistory As EventHistory
    Public Overridable Property Hospital As Hospital
    Public Overridable Property MemberAppointmentResults As ICollection(Of MemberAppointmentResult) = New HashSet(Of MemberAppointmentResult)
    Public Overridable Property MtfReason As MtfReason
    Public Property DiagCodes As Integer
    Public Overridable Property VADiagCdWebgrid As ICollection(Of DiagnosisCode) = New HashSet(Of DiagnosisCode)
End Class

DiagnosisCode.vb

Partial Public Class DiagnosisCode
    Public Property VADiagnosticCodeID As Integer
    Public Property VADiagnosisCode As String
    Public Property DiagnosisCodeDescription As String
    Public Overridable Property MemberAppointmentResults As ICollection(Of MemberAppointmentResult) = New HashSet(Of MemberAppointmentResult)
End Class

MemberAppointmentResult.vb

Partial Public Class MemberAppointmentResult
    Public Property ResultID As Integer
    Public Property AppointmentID As Integer
    Public Property VADiagnosticCodeID As Integer
    Public Overridable Property DiagnosisCode As DiagnosisCode
    Public Overridable Property MemberAppointment As MemberAppointment
End Class

MemberAppointment.vb

Partial Public Class MemberAppointment
    Public Property AppointmentID As Integer
    Public Property MemberEventID As Integer
    Public Property ScheduledVisitDate As Date
    Public Property ScheduledVisitTime As Nullable(Of System.TimeSpan)
    Public Property HospitalID As Integer
    Public Property ActualVisitDate As Nullable(Of Date)
    Public Property ActualVisitTime As Nullable(Of System.TimeSpan)
    Public Property ActualHospitalID As Nullable(Of Integer)
    Public Property IsVisitCompleted As Boolean
    Public Property CurrentDisabilityPercentage As Nullable(Of Byte)
    Public Property MtfReasonCodeID As Integer
    Public Overridable Property EventHistory As EventHistory
    Public Overridable Property Hospital As Hospital
    Public Overridable Property MemberAppointmentResults As ICollection(Of MemberAppointmentResult) = New HashSet(Of MemberAppointmentResult)
    Public Overridable Property MtfReason As MtfReason
End Class

所以这就是我对这个软件的缺乏经验让我浪费了很多时间的地方。我将用我刚刚发现的(经过 6 个月的研究)来回答这个问题,但没有找到任何支持(或反驳)我试图做的事情的答案......我打电话的顺序没有好像支持。

显然你不能这样做: 把这个放在 Create.vbhtml

Using Html.BeginForm("Create", "Appointment", New With {.memId = ViewBag.MemID}, FormMethod.Post, New With {.autocomplete = "off"})
Html.Partial("AddDiagCdResult", ViewBag.NewAppResult)

将其放入 AddDiagCdResult

Using Html.BeginForm("AddDiagCodeResult", "Appointment")
Html.DropDownList("DiagCodes", Nothing, New With {.onchange = "this.form.submit()", .style = "width:575px;"})

...然后期望下拉列表中的“.onchange”激活 AppointmentController 中的 AddDiagCodeResult 函数。

如果将 Html.Partial 语句移动到第一个 Html.BeginForm 语句上方,则 Html.DropDownList 可以正常工作。我不知道为什么它以一种方式而不是另一种方式工作,我找不到答案。

另一种方法是使用多个 Using/End Using 语句来覆盖需要激活不同控制器方法的不同部分。您不必激活 Html.Partial 语句,所有代码都可以在一个视图文件中。