Reputation: 59
I'm trying to pass the model(PresentationModel in my case) as variable to the post method in the controller. However it crashes because my parameter isn't send with it. However this should be optional but it crashes anyway. This is what i got so far.
Model
Public Class ClassA
Public Shared Items As List(Of ClassA) = New List(Of ClassA)
<Required(ErrorMessage:="Required")>
<MinLength(100, ErrorMessage:="to short")>
Public Name As String
<Required(ErrorMessage:="Required")>
Public Age As Integer
Public Message As String
Public Sub New(name As String, age As Integer, message As String)
Me.Name = name
Me.Age = age
Me.Message = message
End Sub
Public Sub AddItem(item As ClassA)
Items.Add(item)
End Sub
End Class
PresentationModel
Public Class PMClassA
Inherits ClassA
Public Extra As String
Public Sub New(name As String, age As Integer, message As String)
MyBase.New(name, age, message)
End Sub
End Class
Controller
<HttpGet>
Function Test() As ActionResult
Dim v As PMClassA = New PMClassA("test", 0, "test")
Dim ListItem As List(Of SelectListItem) = New List(Of SelectListItem)
For i As Integer = 0 To 5
v.AddItem(New ClassA("Name" + i.ToString(), i, "Message" + i.ToString()))
ListItem.Add(New SelectListItem With {.Text = PMClassA.Items.Item(i).Name, .Value = PMClassA.Items.Item(i).Name})
Next i
ViewBag.Variable = v.Name
ViewData("List") = New SelectList(ListItem, "Value", "Text", "")
Return View(v)
End Function
<HttpPost>
Function TestPost(Optional PMClassA As PMClassA = Nothing) As ActionResult
If (String.IsNullOrEmpty(Request.QueryString("PMClassA")) Or String.IsNullOrEmpty(Request.QueryString("Extra"))) Then
Return View("Error")
End If
If (ModelState.IsValid) Then
Return View()
End If
Return View
View
@ModelType TestApp.PMClassA
<h2>@ViewBag.Variable</h2>
@Using (Html.BeginForm("TestPost", "Home", FormMethod.Post))
@Html.AntiForgeryToken()
@<div class="form-horizontal">
<h4>ClassA</h4>
@Html.ValidationMessageFor(Function(model) model.Name)
<br />
@Html.ValidationMessageFor(Function(model) model.Age)
<br />
@Html.ValidationMessageFor(Function(model) model.Message)
<input type="hidden" value="extra stuff" name="Extra" />
<hr />
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<p>@Html.DropDownListFor(Function(model) model.Name, ViewData("List"))</p>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Opslaan" name="Opslaan" class="btn btn-default" />
</div>
</div>
</div>
End Using
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Why do i get in my post method Nothing as value? I should get the PMClassA object with the variables: "Name, Age, Message" from ClassA and "Extra" from PMClassA.
Edit
I can however get the info for Name and Extra.
Function TestPost(Optional Name As String = "", Optional Extra As String = "") As ActionResult
If (String.IsNullOrEmpty(Request.QueryString("Name")) Or String.IsNullOrEmpty(Request.QueryString("Extra"))) Then
Return View("Error")
End If
However why do i get "Name" and "Extra" and not the PMClassA object with "Name" and "Extra" filled in?
Upvotes: 0
Views: 45
Reputation: 63126
Looking at your code here, it appears that your interface doesn't match the posting requirements, and additionally for the post method you wouldn't need it to be optional in this case as the expectation is that you are in fact getting the desired model back.
A few other observations.
Upvotes: 1