Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net MVC Composite class custom model binding
    text
    copied!<p>I've searched all over the web but not finding the solution for the following problem:</p> <p>Say I have three ViewModel classes</p> <pre><code>public class ViewModelNewPerson { public string PersonName; public string Address; public string EyeColor; //etc } public class ViewModelSelectPerson { public int SelectedPersonId; } public class ViewModelComposite { public ViewModelSelectPerson selectViewModel; public ViewModelNewPerson newPersonViewModel; } </code></pre> <p>and I want to do the following things:</p> <p>In the <code>Controller</code> I want to create a <code>GET</code> <code>Action</code> which uses the class <code>ViewModelComposite</code> as its Get model, and in the view I want the user choose from the following two available actions: to choose a existed person, and to add a new person as the selected value.</p> <p>So I need to create two forms in the <code>View</code>, and there would be two <code>POST</code> <code>Action</code>s added to the <code>Controller</code> using the Post model of class <code>ViewModelNewPerson</code> and <code>ViewModelSelectPerson</code>.</p> <p>My question is, how can I do the manual model binding using a Custom Model Binder that can convert the Composite class of <code>ViewModelComposite</code> to <code>ViewModelNewPerson</code> in the <code>Action</code> of create a new person, and to <code>ViewModelSelectPerson</code> in the <code>Action</code> of select an existing person?</p> <p>EDIT:</p> <p>Now I have an idea of decomposing the class <code>ViewModelComposite</code> and declare every property in the two classes into the composite class, and the default model binder will do the trick, I think. But that'll drop the composite pattern, and is not something I wanted.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload