• remote机制的AdditionalFields


    有时为了在做远程机制时,多使用一个参数,可以加上AdditionalFields字段,该字段可以自定义数据,交给服务器验证。

    样例代码:

    类:

    public class ColumnVM : BaseObj
        {
            [Required(ErrorMessage = "请填写栏目的名称"), Remote("CheckColumnName", "Course", ErrorMessage = "已存在相同栏目", AdditionalFields = "ActionMethod")]
            public string Name { get; set; }
            public string IsParent { get; set; }
            public string ParentId { get; set; }
            public string ImageURL { get; set; }
        }

    控制器:

            public JsonResult CheckColumnName(string name, string actionMethod)//actionMethod是为了判断验证的方法是来自哪一个表单
            {
                bool result = false;
                //不包含文件夹名,则校验成功
                List<String> columnNames = columnBLL.GetAll().Select(a => a.Name).ToList();
                if (actionMethod == "Edit")
                {
                    columnNames.Remove(name);
                }
                result = !columnNames.Contains(name);
                return Json(result, JsonRequestBehavior.AllowGet);
            }

    前端:

     @using (Html.BeginForm("EditColumn", "Course", FormMethod.Post))//为了方便,本样例只写一个表单,另一个add表单内容相仿。
        {
            <input type="hidden" name="ActionMethod" value="Edit" />
            @Html.HiddenFor(a => a.Id)       
            <table class="t-tableAddAndEdit">
                <tr>
                    <th>
                        项目
                    </th>
                    <th>
                        详情
                    </th>
                </tr>
                <tr>
                    <td>
                        当前目录
                    </td>
                    <td id="editDirectory" style="color: #269DDC">
                        Root>
                    </td>
                </tr>
                <tr>
                    <td>
                        课程栏目名称
                    </td>
                    <td id="editColumnName">
                        @Html.TextBoxFor(model => model.Name, new { @class = "editColumn" })
                        @Html.ValidationMessageFor(model => model.Name)
                    </td>
                </tr>
            </table>
            <div style="height: 30px">
            </div>
            <input style="float: left; margin-left: 20px;" type="submit" value="确定" class="submitBtn" />
        }

    该样例实现依据传过来的方法的不同(是add还是edit),做相应的操作,判断文件名是否存在重复问题。

  • 相关阅读:
    [模板]RMQ(冲刺准备中)
    洛谷 P2569[SCOI2010]股票交易(动规+单调队列)
    洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
    粗略了解fill与fill_n
    计蒜客D2T2 蒜头君的排序(动态维护树状数组)
    洛谷 P3478 [POI2008]STA-Station
    洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network
    洛谷 P3112 [USACO14DEC]后卫马克Guard Mark
    洛谷 P3092 [USACO13NOV]没有找零No Change
    洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环
  • 原文地址:https://www.cnblogs.com/Benjamin/p/2985181.html
Copyright © 2020-2023  润新知