日期:2014-05-17  浏览次数:20525 次

关于MVC3中 Remote 的参数取值问题
网上看到的例子,当将model当成一个属性时,验证方法传过来的参数就为空?源码如下
model类:
public class User
    {
        [Required(ErrorMessage="姓名不能为空")]
        [Remote("IsUnique_Available","Home")]
        public string Name { get; set; }
    }

封装后的Users类
 public class EditUserVM
    {
        /// <summary>
        /// User VM
        /// </summary>
        public User Users
        { 
            get;
            set;
        }
    }
view页面:
@model MvcRemoteValidation.ViewModels.UserViewModels.EditUserVM

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Users.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Users.Name)
            @Html.ValidationMessageFor(model => model.Users.Name)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>