21194

ASP.NET MVC razor view, post to a different model from the original model binding?

Question:

Well I have a group controller and view in the project, in which the model binding is GroupViewModel. But the group page is complex, and users will be able to make discussion topics. On this group view page, I have forms that allow users to post topics/replies. The model used for these forms can be TopicViewModel or ReplyViewModel, but the original model binding is only for GroupViewModel. It is declared at the beginning of the cshtml page:

@model MyProject.ViewModels.GroupBrowseViewModel

So I wonder, is it possible to have forms bind to different view model from the one declared at the top? If so, how to achieve this?

Answer1:

Model binding really has nothing to do with the model used in your razor view. At least not technically.

The first thing you have to understand is that there is no magic here. This is straight HTTP posted values, and if you don't understand how HTTP posting works, I suggest you read up on it. It's just a series of name/value pairs.

When you post, the routing framework looks at the selected action method, and the parameters that method takes, then it creates new instances of those parameters, and tries to match them up with similarly named values from the posted values.

So, in other words, there is no direct connection here between the model you use on the page, and the model used in the posted controller action. It's all based on naming convention. This naming convention is "helped" by the model you declare on the page, and the Html helpers create form fields with names that match up to model entries so that the model binder can figure these out more easily.

So, what this means is that in order to post to a different action, with a different model, all you need are fields in your form that have the names that the new model expects.

This can be done in a number of ways, from defining those fields manually, to using Partial view in which you pass an instance of the model you intend to post to as the model parameter.

Answer2:

You can use 2 partial views in the same view, and switch from the controller which one to show.

Or you can make a new ViewModel and this view model will contain references to both of your views, and in the view html based on your logic your can switch reading from any child ViewModel.

Recommend

  • Using an io.WriteSeeker without a File in Go
  • Prepare C-based Cython package to publish on pypi
  • $http.get of json file always returns 404
  • How to create temporary files on linux that will automatically clean up after themselves no matter w
  • Padding time-series subsequences for LSTM-RNN training
  • UITableViewController auto-scrolling stops taking into account keyboard when shown from a UISplitVie
  • Linq memory question
  • Are PDF box coordinates relative or absolute?
  • Fast and efficient way to expand a dataset in R
  • Not able to access the database file through adb shell… shows permission denied
  • Maven compilation complication due to complex folder structure
  • how do you uninstall an xampp installation that refuses to uninstall?
  • How to parse this JSON in PHP?
  • how can i open a webpage in a webview from a fragment with buttons?
  • Get Network Interface Names and Set All to DHCP Batch Script
  • GLSurfaceView Zoom(PINCH ZOOM)
  • Constructor-Prototype cyclic reference JavaScript
  • How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in t
  • How to order the ties in data so that the previously observed value appears first
  • In Moment.js, how do you get the date of the next occurrence of a specific month (ex: 'next Jan
  • Get last 15 lines from a large file in SFTP with phpseclib [duplicate]
  • Expression.Call GroupBy then Select and Count()?
  • Filtering out choiceless polls in the Django tutorial causes polls in the index to duplicate
  • How to run Daphne Server (Django Channels) & workers in the background?
  • Adding Dynamic Row and Data on Checkbox Click
  • How do you run a synchronous timer in C#?
  • How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?
  • C++11: Why rvalue reference parameter implicitly converted to lvalue
  • System.Drawing.DrawString() weird wrapping of long string
  • Allowing audio files in Spring MVC 3.0?
  • First dynamically-added TinyMCE editor displays, others do not
  • How to split wav file into two or more parts using c#
  • Content-Type alternative in MQTT
  • Comparing variables with strings bash
  • Cloud Code: Creating a Parse.File from URL
  • Change cell value based on cell color in google spreadsheet
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Update cell query for Excel ADO from Delphi
  • Call Microservice from another Microservice within Docker
  • Angular 4: Responsive Grid List