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



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

我已经搜索了Beginform, Partial, Controller, viewbag, Model, ViewModel, overloading…你能想到的……我查过了。微软的文档通过使用定义中定义的单词来定义术语,因此这并不能帮助我理解如何将对象连接在一起以做我想做的事情。

我寻找的例子看起来像我在做什么,但似乎没有人做我是;我找到的所有东西要么是讨论每个单独的部分,要么是单个应用软件的解决方案。

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

AppointmentController:

<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

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

所以这就是我对这个软件的经验不足花费我这么多时间的地方。我将用我刚刚发现的(经过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&;

如果你移动Html。第一个Html上面的部分语句。BeginForm语句,Html。下拉列表工作得很好。我不知道为什么它是这样而不是那样的,我找不到答案。