微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

.net 编程新手。我正在使用 Visual Studios 2017,在 vb.net 中编程,使用 MVC 设置(无论这意味着什么),以及使用 Razor 的 html 网页 - 我希望这对你来说足够了。我正在更新别人的代码,所以这不是从头开始编写的。我正在尝试在创建屏幕上添加一个动态网格(看起来像一个 Excel 电子表格),其中第一行/列项是下拉列表,行中的其他列是数据库表中的字段(sql server 2017 v18) . Controller 中的 http:Get Create 函数已经存在,用于显示用户输入内容的字段(网格除外),而 http:Post Create 存在用于将数据放入数据库。 我想出了如何在屏幕上放置局部视图,以便它显示下拉框和(据说)我想要显示数据库表中的字段。我怀疑我的网格是否正确,但我知道每次我从下拉框中选择一个项目时,它会自动转到 http:Post Create 而不是我希望它转到的功能,以便我可以得到表中的数据以显示我想要的项目。我在整个网络上搜索了答案,但我唯一能找到的是 Web Screens 或 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

创建.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",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

解决方法

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

,

所以这就是我对这个软件的缺乏经验让我花费了很多时间的地方。我将用我刚刚发现的(经过 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 可以正常工作。我不知道为什么它以一种方式起作用,而另一种不起作用,我找不到答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。