ASP.NET MVC Model/ViewModel Validation

℡╲_俬逩灬. 提交于 2019-12-04 08:09:10

If you use partials, and pass in the subtype, you still need to qualify. See as follows:

<%@ Control Language="C#" 
                   Inherits="System.Web.Mvc.ViewUserControl<MvcWeb.Models.OrderDetail>" %>

<% using (Html.BeginForm()) { %>

    <fieldset>
        <legend>Fields</legend>
            <%= Html.Hidden("OrderId", Model.OrderId) %>
            <%= Html.Hidden("ProductId", Model.ProductId)%>
        <p>
            <label for="Quantity">Quantity:</label>
            <%= Html.TextBox("OrderDetails.Quantity", Model.Quantity)%>
            <%= Html.ValidationMessage("OrderDetails.Quantity", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

Notice that the type is OrderDetails, but I'm still prefixing that to deal with validation messages.

Can you post your xval helper code, and some of your Html.Helpers?

It takes the entity and the prefix, so I don't see why the structure within your view model should make any difference. Something like:

<%= Html.ClientSideValidation<Foo>("Foo") %>
<%= Html.ClientSideValidation<Bar>("Foo.Bar") %>

James

If i read this correctly you are putting the DataAnnotations on the linq to sql class then populating your viewmodel properties with the ones from you linq to sql class.

To get this to work with xval you would need to put the DataAnnotations on the view model properties. As far as i can tell from xvals code it dosen't look beyond the public properties for any validation information (someone please correct me if im wrong here).

If you wanted to make the validation transparent between your model and viewmodel you could go down the route of using postsharp to bind the attributes but this could be a lot of work for little gain if you program is small.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!