I am new to MVC, and although I tried to follow best practices, I believe that I don’t understand some of the basics either
-a. Use of models and viewing models respectively -b. Transferring models to the controller for verification purposes.
The general goal of my program is to select a stored procedure from the list of stored procedures, create a form in which the user can fill in all the relevant input variables, and then execute this stored procedure. The tool is intended for non-technical people, so in the end I will have to do a big input check.
Thus, I have four models: A Script Model, Parameter Model, Enum Parameter Model and Request Model, and two viewing models: a paramviewmodel that generates a fillable form, and a scriptview model that creates a list filled with possible Script options. I use a pre-written database system to populate my view modes in the init method in my controller (which I'm not sure if this is the right way to do this?).
The presented models are as follows:
Imports System
Imports System.Collections.Generic
Public Class ScriptViewModel
Public Property SelectedItemId As Integer
Public Property Scripts As DataTable
Public Property ScriptList As List(Of ScriptModel)
Sub InitScriptData()
' Fills the data from an outside database.
' And fills out the properties above. Does nothing else
End Sub
End Class
Other
Imports System
Imports System.Collections.Generic
Public Class ParamViewModel
Public Property Params As DataTable
Public Property ParamEnums As DataTable
Public Property ParameterList As List(Of ParameterModel)
Public Property ParamEnumList As List(Of ParamEnumModel)
Public Property ParamEnumDictionary As Dictionary(Of Integer, List(Of ParamEnumModel))
Sub InitParamData(ByVal Script_Index As String)
' Uses an outside database
' Fills out all the above variables
End Sub
End Class
Their related views look like this:
Script:
@ModelType Scripter.ScriptViewModel
@Html.ValidationSummary("Please correct the errors and try again.")
@Using (Html.BeginForm("ParamChoice", "Parameter", FormMethod.Post))
@<div>
@Html.ListBox("ScriptListBox", New SelectList(Model.ScriptList, "Script_Index", "CustomerScriptName"), New With {.class = "LargeListBox", .title = "LargeListBox"})
</div>
@<input type="submit" value="Execute Script" />
End Using
ParamChoice:
@ModelType Scripter.ParamViewModel
@Code
ViewData("Title") = "ParamChoice"
End Code
<h2>ParamChoice</h2>
<!-- Helper Method defined in App_Code that creates a form with a dynamic number of fields of appropriate input types -->
@HelperMethods.CreateVariableInputParameterFields(Model.ParameterList, Model.ParamEnumDictionary)
Said Helper (this is where my main confusion lies) (Note that inheriting helperpage refers to a class that allows me to use htmlhelpers in @helper in app_code)
@inherits Scripter.HelperPage
@Imports System.Web.Mvc
@Imports System.Web.Mvc.Html
@helper CreateVariableInputParameterFields(ByVal ParamList As List(Of Scripter.ParameterModel), ByVal EnumDictionary As Dictionary(Of Integer, List(Of Scripter.ParamEnumModel)))
Dim item As Scripter.ParameterModel
@Html.ValidationSummary("Please correct the errors and try again.")
Using (Html.BeginForm("QueryServer", "Query", FormMethod.Post))
Dim iterator As Integer = 0
Dim ParamValue(ParamList.Count) As String
Dim ParamName(ParamList.Count) As String
Dim ParamType(ParamList.Count) As String
For Each item In ParamList
If (String.Compare(item.ParamType, "Int") = 0 Or String.Compare(item.ParamType, "String") = 0) Then
@<br />
@Html.Label(item.ParamName)
@Html.TextBox("ParamValue", Nothing, New With {.class = "text-box", .id = CStr(iterator)})
@Html.Hidden("ParamName", item.ParamName, New With {.id = CStr(iterator)})
@Html.Hidden("ParamType", item.ParamType, New With {.id = CStr(iterator)})
iterator += 1
ElseIf (String.Compare(item.ParamType.ToString, "Enum") = 0) Then
Dim tlist = EnumDictionary.Item(item.Param_Index)
@<br />
@Html.Label("label", item.ParamName, New With {.class = "display-label"})
@Html.DropDownList("ParamValue", New SelectList(tlist, "EnumValue", "EnumValue"), New With {.id = CStr(iterator)})
@Html.Hidden("ParamName", item.ParamName, New With {.id = CStr(iterator)})
@Html.Hidden("ParamType", item.ParamType, New With {.id = CStr(iterator)})
iterator += 1
Else
@<br />
@Html.Label("label", item.ParamName, New With {.class = "display-label"})
@Html.CheckBox("ParamValue", Nothing, New With {.id = CStr(iterator)})
@Html.Hidden("ParamName", item.ParamName, New With {.id = CStr(iterator)})
@Html.Hidden("ParamType", item.ParamType, New With {.id = CStr(iterator)})
iterator += 1
End If
Next
@Html.Hidden("Script_Index", ParamList.Item(0).Script_Index)
@<div>
<input type="submit" value="Query Server"/>
</div>
Html.EndForm()
End Using
End helper
Script Controller as an example of what I was doing:
Namespace Scripter
Public Class ScriptController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Dim Test As New ScriptViewModel
Test.InitScriptData()
Return View(Test)
End Function
End Class
End Namespace
, , -, . , , , - .
. -, , init, , mvc ( , ?). , , htmlhelper, ( paramviewmodel), ? , , , , . . - ?
, . ( vb.net, mvc) . , , .
: ?