I'm on a asp.net MVC 2 project and have a view that inherits from System.Web.Mvc.ViewPage<dynamic>. I would like to do something like below:
public ActionResult Index()
{
dynamic model = new {Value1= string.Empty, Value2= string.Empty};
return View(model);
}
[HttpPost]
public ActionResult Index(dynamic model)
{
var value1 = model.Value1;
var value2 = model.Value2;
// do something here.
}
For my view, I currently have the below:
<% using(Html.BeginForm("Index", "Test"))
{
%>
<div>
<label for="Value1">Value1:</label>
<%=Html.TextBox("Value1", Model.Value1 as string) %>
</div>
<div>
<label for="Value2">Value2:</label>
<%=Html.Password("Value2", Model.Value2 as string) %>
</div>
<div>
<input type="submit" value="Submit" />
</div>
<% } %>
The above code yields an error of "'object does not contain a definition of 'Value1'" and highlights the Html.TextBox line for Value1.
I have attempted to just write my own html form and input tags (making sure to include both name and id attributes) and setting the value to Model.Value1 and Model.Value2. This works to render the page (and test values); however, upon submission I get the same error as before.
Is it possible to use anonymous and/or dynamic types for my ViewModels in ASP.Net MVC2 or am I forced to write a ton of DTOs which I'm hoping to avoid.