Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>My question is can I post data to NewModel from my form? </code></pre> <p>The short answer is yes you can post the form to any controller action on any controller related to any Model in your application.</p> <p>For example, for your form to post to the "<code>Add</code>" action on the <code>NewModel</code> controller:</p> <pre><code>@using (Html.BeginForm("Add", "NewModel")) { @Html.Hidden("ID", "1") @Html.Hidden("UserID", "44") @Html.TextArea("Description") } </code></pre> <p>Since your view is strongly typed to your <code>Car</code> model, You can either change this and send a ViewModel to your view whose type matches the model your updating (as <a href="https://stackoverflow.com/a/15094236/2040555">Darin demonstrated</a>), or you'll need to map the post data from <code>Car</code> onto <code>NewModel</code> in your controller:</p> <p>On the <code>CarController</code>'s <code>Add</code> action (Post) :</p> <pre><code>[HttpPost] public PartialViewResult Add(Car model) { //now map attribute values from the Car model onto //corresponding attributes of an instance of NewModel NewModel new = new NewModel(); new.ID = model.ID; new.UserID = model.UserID; new.Desc = model.Description; //etc... //update your model/db _db.Add(new); //Redirect however you wish... } </code></pre> <p>Also, check out <a href="https://github.com/AutoMapper/AutoMapper" rel="nofollow noreferrer">AutoMapper</a>, an object-to-object mapper which automates mapping of ViewModels onto Models and vice versa.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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