1
votes

Please consider I have 2 forms which will put in single view which are student form and teacher form. Each has different form action. I need to pass these 2 models from controller at once. So i decide to take the 2 into a new model class:

Public Class StudentTeacher
    Public Student As ClsStudent
    Public Teacher As ClsTeacher
End Class

And send the model from the controller:

Return View(New StudentTeacher With {.Student = New ClsStudent, .Teacher = New ClsTeacher })

And in the view I have 2 partial view one for student form and one for teacher form. For the student form would be like this:

@ModelType Model.StudentTeacher
@Using Html.Beginform()
     ....
     @Html.TextBoxFor(Function(m) m.Student.name)
     ....
End Using

This view works good only the problem is the id for each element will include the model name, so the input for student name will be:

<input id="student.name">

So when the form got submitted, the controller will receive it as model of 'StudentTeacher' instead of 'Student'. What I want for this student form is send the student model only. The teacher can be sent in different form in the same view. Any suggestion to fix this? Thank you very much...!

1
Please let me know if it solved the problem. If it did, please mark as answer my reply. Thanks...Murat Yıldız

1 Answers

1
votes

There are 2 ways I can suggest to fix the problem:

1) You can use a ViewModel by combaining both of two models in it. Then return this ViewModel from Controller to the View. After changing data on the View then return this ViewModel to the Controller again. If you want to make different action with this data you can make this in the Controller instead of calling different method from the View.

2) As you did, you can use partial views and then send each model to the related partial views in the main view. However, when you submit the form you should pass the form parameters at one time as two parameters (model I and model II). Then you get the form data in controller (controller accepts these two arguments: model I and model II as well).

Hope this helps...