Let’s say you’re developing your own stackoverflow in ASP.NET MVC 🙂
You’d like to have a functionality to answer questions. So, the view should contain original question details (title, text, etc) and it should also contain the form to submit the answer. In order to render the view, you should provide both original question object and new empty answer object. You then want only answer to be posted back.
How would you define the type for this view? Would you implement a view model specific for this view that would contain both the question and an empty answer? What’s the common approach here?
Ideas I have are:
-
Using a special view model object that contains both question and answer. View is bound to this object. When rendering the view, question details are used. When posting back, the object I get at controller only has answer-related fields populated.
-
Using
ViewBagto pass question object. View is bound to answer model. -
?
(no AJAX please, just the very basic scenario)
Your question doesn’t make much sense. Your question would not be posted back because only form controls are posted, such as inputs, button values, etc.. Your question is just html text, and does not get posted.