0

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.

JamesEggers
  • 12,885
  • 14
  • 59
  • 86
  • Using application/view/data model entity classes is still better because of auto-validation. Anonymous classes don't give that pleasure anyway. – Robert Koritnik Feb 11 '11 at 15:36
  • I agree with Robert, just about everything I've heard and read about ASP.NET MVC suggests strongly typed views (including things I heard during Tuesday's MVC Conf). – Mayo Feb 11 '11 at 15:40
  • I understand the benefits of strongly typed viewmodels and what not that way. This is more of a Proof of Concept at this point just to see if it works. – JamesEggers Feb 11 '11 at 15:42

2 Answers2

2

Yes, it's possible, if you declare your view as having a dynamic model:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

That said, if you use MVC 2's default templates (Html.EditorForModel()) you don't even have to do that.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • I actually have that but SO took out the generic attribute. Working on fixing such now. It still isn't working even with that though. – JamesEggers Feb 11 '11 at 15:46
  • That should work, but try it without the `Html.TextBox`, as I'm not sure they support the dynamic object. Like I said, though, I'd use `EditorForModel()` here as it removes the need to keep controller and view in sync without static type checking. – Craig Stuntz Feb 11 '11 at 15:50
  • Doesn't look like EditorForModel() nor the Html helpers actually support dynamic and anonymous types. Looks like I'm stuck with DTOs when using MVC2. - Thanks – JamesEggers Feb 11 '11 at 15:59
0

On .NET 4.0 Anonymous types can easily be converted to ExpandoObjects and thus all the problems is fixed with the overhead of the conversion itself. Check out here

Community
  • 1
  • 1
DATEx2
  • 3,585
  • 1
  • 24
  • 26