I have a model with a controller and views. In the "create" view I added a field that does not belong to the model.
How can I read the field in the controller?
Thank you very much.
I have a model with a controller and views. In the "create" view I added a field that does not belong to the model.
How can I read the field in the controller?
Thank you very much.
 
    
    You can access properties that are not in your View Model by accessing Request.Form["Property"]. Please see the following example:
https://dotnetfiddle.net/riyOjb
It is recommended that you do you view models, however.
public class SampleViewModel
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new SampleViewModel();
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(SampleViewModel model)
    {
        // model.Property1 is accessable here
        // as well as model.Property2
        // but if you want something not in the view model, use Request.Form
        ViewData["CustomProperty"] = Request.Form["CustomProperty"];
        return View(model);
    }
}
@model MvcApp.SampleViewModel
@using(Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Property1)<br /><br />
    @Html.TextBoxFor(m => m.Property2)<br /><br />
    <input type="text" name="CustomProperty" id="CustomProperty" /><br /><br />
    <button type="submit" class="btn">Submit</button>
}
<h2>Submitted Data</h2>
@Model.Property1<br />
@Model.Property2<br />
@ViewData["CustomProperty"]
