开发者

How can I use a property named 'Model' in my viewmodel class?

开发者 https://www.devze.com 2023-02-12 04:00 出处:网络
I have a very simple viewmodel class, and a strongly typed view (which uses this class as a viewmodel).

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; }
   开发者_Go百科 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 property named "Model" in there.

When I just do this in the view:

<%= Html.EditorFor(m => m) %>

I get a nice form with all the properties of MatchModelRequest. No problem at all (also not with the modelbinding, when retrieving the values back in a MatchModelRequest object).

But when I split them out in individual fields like this on the view:

<div class="editor-field">
    <%= Html.TextBoxFor(m => m.Model)%>
    <%= Html.ValidationMessageFor(m => m.Model)%>
</div>

I get an unhandled exception System.ArgumentException: Value cannot be null or empty, on the first line above where m.Model is used.

I suppose this has something to do with the Model property on ViewPage, but I don't see what I can do to solve this problem other than just giving that property another name.

Giving that property another name is possible now in my case, but I would like to keep it named "Model". And it's not always possible to change the properties of your model.

Edit

I've been further investigating this, and apparently the part where this goes wrong is situated in System.Web.Mvc.ExpressionHelper in the GetExpressionText(LambdaExpression expression) method. There there are the following lines:

// 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 stripped of... so I guess this is by design then, and I don't have another option then to rename my property?

Suggestions are still welcome.

Btw, this is MVC 2. Could somebody check if this code is still present in MVC 3?


Yeah, that's right. I've looked at the MVC 3 RTM source and there is two places where the "Model" is removed from the expression. First one is when we use the string expression like @Html.TextBoxFor("model").

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

The other is on the GetExpressionText(LambdaExpression expression) as you said before.

    // 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 by now 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号