I am having trouble trying to combine two models for use with a view. I have created a consolidated model object:
Public Class BusinessFormModel
Public Property busModel As Business
Public Property busMetaModel As BusinessMeta
Public Sub New()
busModel = New Business
busMetaModel = New BusinessMeta
End Sub
End Class
For my View I have a form with something like:
@ModelType business.BusinessFormModel
@Html.TextBox("business_category", Model.busModel.business_category)
..
@Html.TextBox("business_name", Model.busMetaModel.business_name)
..
@Html.TextBox("business_description", Model.busMetaModel.business_description)
My controller looks like this:
Function ShowForm() As ActionResult
Dim model As new BusinessFormModel
model.busModel.PopulateFromId(2)
model.busMetaModel.PopulateFromId(2)
Return View(model)
End Function
Function Submit(ByVal model As BusinessFormModel) As String
model.busModel.UpdateCategory()
...
model.busMetaModel.UpdateBusinessDescription()
Return "Submitted"
End Function
Basically the ShowForm will populate the object with values from my database based on the id. The id is also a property in this model. Then when the form submits I have functions to update the particular field based on the id. However, seems like when Submit() is called the object that is passed contains Nothing values. I should at least be getting data that was bound to the HTML helper controls, right?
I’m not sure if I am doing this right since when I submit the form, the model object that is passed back to the controller does not contain any data. Am I executing the right statements in the view?
Thanks.
You need to look into
ViewModelwhich will allow you to have multiple models and other business logic in a single view. ViewModel example exerciseTaken as an example. You will have a separate
ViewModelclass that exposes your model(s) to your view besides yourController.ViewModel
Controller
In your
View, you can still refer to youModelobject the same way but it now contains all theModelsand other data that now can be used. Hope this helps.