If I have a view that has a model, lets say Car..
@model Project.Car
inside that view I want to create a form that sends data to a new model
@using (Html.BeginForm("Add", "Controller"))
{
@Html.Hidden("ID", "1")
@Html.Hidden("UserID", "44")
@Html.TextArea("Description")
}
I've noticed that if my action is defined with my ViewModel it does not work (model is always null):
[HttpPost]
public PartialViewResult Add(ViewModels.NewModel model)
However, if I use a FormCollection it works:
[HttpPost]
public PartialViewResult Add(FormCollection formCollection)
Here is the ViewModel:
public class NewModel
{
public int ID { get; set; }
public int UserID { get; set; }
public string Description { get; set; }
}
My question is can I post data to NewModel from my form? The View that it sits on is correct to be tied to Project.Car. Its a small form on the page that needs to post a different set of data that has nothing to do with Project.Car.
There are four various methods to create the ASP.NET MVC Forms with easy way, they are as follows, In the user interface view, there will be textbox field and submit buttons, it contains three textbox fields created for getting the values for ID of the person, name, and city of the person by using the Html.TextBoxFor method.
ASP.NET MVC Core will bind the form data to your model automatically. This also means we won’t find ourselves constantly updating the Index action every time we need to handle another value submitted via the form.
Submit your form with the developer tools open (F12) and you’ll see the form post in the network tab. Click the relevant request and check out the Form Data section. In this example you can see that firstName has been submitted as form data and so should be available to ASP.NET Core.
Wire up your inputs (text boxes etc) to something on the model (using Tag Helpers) and the value entered by the user will be submitted as formdata when the user submits the form. ASP.NET Core’s model binding will then kick in and assign the posted values to an instance of the Model.
Yes, you can strongly type a view to one model and POST it to another model.
In doing so you have two options:
Manually provide correct names for each input field, so that the default binder will understand it and create the model (example).
While this works, it also means you have to keep an eye on typos and you will not get any compile-time errors if you misspell a property name.
Manually create a HTML helper in the view bound to the new model type. It will then properly generate the HTML for you.
In order to construct the helper, you need a wrapper object that would expose the instance of your model in the form of the IViewDataContainer
interface. You can define that wrapper anywhere, including the model itself:
public class NewModel
{
public int ID { get; set; }
public int UserID { get; set; }
public string Description { get; set; }
public class WrapperForHtmlHelper : System.Web.Mvc.IViewDataContainer
{
public System.Web.Mvc.ViewDataDictionary ViewData { get; set; }
public WrapperForHtmlHelper(NewModel value)
{
this.ViewData = new System.Web.Mvc.ViewDataDictionary<NewModel>(value);
}
}
}
Then in a view you create a helper bound to an instance of NewModel
:
var ModelToPost = new YourApp.Models.NewModel() { ID = 1, UserID = 43, Description = "" }
var hlp = new HtmlHelper<YourApp.Models.NewModel>
(this.ViewContext,
new YourApp.Models.NewModel.WrapperForHtmlHelper(ModelToPost)
);
And then you use the helper as usual:
@hlp.HiddenFor(m => m.ID)
@hlp.HiddenFor(m => m.UserID)
@hlp.TextAreaFor(m => m.Description)
Then your PartialViewResult Add(ViewModels.NewModel model)
will properly receive the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With