I have a very simple viewmodel class and a strongly typed view (which uses this class as a viewmodel).
public class MatchModelRequest { public string Country { get; set; } public string Make { get; set; } public string Model { get; set; } public DateTime RegDate { get; set; } public int EnginePower { get; set; } public string FuelType { get; set; } public string BodyType { get; set; } public string Transmission { get; set; } public int Cc { get; set; } public int Doors { get; set; } }
The problem is that there is a "Model" property.
When I just do this in a view:
<%= Html.EditorFor(m => m) %>
I get a nice shape with all the MatchModelRequest properties. There are no problems at all (also not with model binding, when returning values back to the MatchModelRequest object).
But when I separate them in separate fields similar to this in the view:
<div class="editor-field"> <%= Html.TextBoxFor(m => m.Model)%> <%= Html.ValidationMessageFor(m => m.Model)%> </div>
I get an unhandled System.ArgumentException: Value cannot be null or empty exception, in the first line above where m.Model is used.
I suppose this has something to do with the Model property on the ViewPage , but I don’t see what I can do to solve this problem, except to just give it a different name.
Providing this property is now possible in my case, but I would like to save it under the name "Model". And it’s not always possible to change the properties of your model.
Edit
I studied this, and apparently the part where this happens is in System.Web.Mvc.ExpressionHelper in the GetExpressionText method (LambdaExpression expression). The following lines exist:
// If it starts with "model", then strip that away if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) { nameParts.Pop(); }
And this is where the actual “model” part is deprived ... so I assume it is by design, and I have no other option to rename my property?
Suggestions are still welcome.
Btw, this is MVC 2. Can anyone check if this code is present in MVC 3?