How can I use a property called "Model" in my viewmodel class?

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?

+4
source share
1 answer

Yes it is. I looked at the source of MVC 3 RTM and there are two places where the “Model” is removed from the expression. Firstly, when we use a string expression, for example @Html.TextBoxFor("model") .

  public static string GetExpressionText(string expression) { return String.Equals(expression, "model", StringComparison.OrdinalIgnoreCase) ? String.Empty // If it exactly "model", then give them an empty string, to replicate the lambda behavior : expression; } 

The other is on GetExpressionText(LambdaExpression expression) , as you said earlier.

  // If it starts with "model", then strip that away if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) { nameParts.Pop(); } 

So, I think the best solution at the moment is to rename your property :)

UPDATE: Actually, it works on Mvc3, I just tested it. There is a fix in the code, see This:

 else if (part.NodeType == ExpressionType.Parameter) { // Dev10 Bug #907611 // When the expression is parameter based (m => m.Something...), we'll push an empty // string onto the stack and stop evaluating. The extra empty string makes sure that // we don't accidentally cut off too much of m => m.Model. nameParts.Push(String.Empty); part = null; } else { break; } 
+3
source

Source: https://habr.com/ru/post/1340636/


All Articles