0

我正在建立一个博客网站。我希望用户能够编辑他们的帖子。我需要更改 HTML 帮助程序的名称以匹配我的模型,以便可以使用远程验证。

模型

        [RegularExpression("[a-z]{1,50}", ErrorMessage = "URL String must be in lowercase")]
        [Required(ErrorMessage = "Unique URL is required")]
        [Remote("doesURLExist", "Post", HttpMethod = "POST", 
        ErrorMessage = "URL already exists. Please enter a different URL.")]
        public string URLString { get; set; }

HTML,使用 viewbag 传递我预先填充的数据。

<div class="form-group">
        @Html.LabelFor(model => model.post.URLString, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.post.URLString, new { htmlAttributes = new { @Value = ViewBag.postURL, @class = "form-control", @name = "URLString" } })
            @Html.ValidationMessageFor(model => model.post.URLString, "", new { @class = "text-danger" })
        </div>
</div>

预先填充的字段像这样工作得很好,但我的远程验证不起作用。name 属性需要是“URLString”,但它以 post.URLString 的形式出现,不能在我的远程方法中使用。

这是我检查现有 URLStrings 的远程方法

    [HttpPost]
    public JsonResult doesURLExist(String URLString)
    {
        var allposts = _unitOfWork.PostRepository.Get();

        if (allposts.Count(p => p.URLString == URLString) == 0)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }


        return Json(false, JsonRequestBehavior.AllowGet);
    }

我已经使用原始 HTML 和手动更改名称属性进行远程验证。

这是我在 google chrome 中查看源代码时助手输出的原始 html。我复制了它并更改了名称。

<div class="form-group">
        <label class="control-label col-md-2" for="post_URLString">URLString</label>
        <div class="col-md-10">
            <input class="form-control text-box single-line" data-val="true" 
                   data-val-regex="URL String must be in lowercase" data-val-regex-pattern="[a-z]{1,50}" 
                   data-val-remote="URL already exists. Please enter a different URL." data-val-remote-additionalfields="" data-val-remote-type="POST" data-val-remote-url="/Post/doesURLExist" 
                   data-val-required="Unique URL is required" id="post_URLString" name="URLString" type="text" value=  />
            <span class="field-validation-valid text-danger" data-valmsg-for="URLString" data-valmsg-replace="true"></span>
        </div>
    </div>

所以效果很好!问题是我不能使用我的 viewbag 来预填充数据。所以我想我有 2 个问题,让我们解决更简单的问题。1. 如何在 HTML 值字段中获取模型数据。value = Model.post.URLString 不起作用。

  1. 如何覆盖 HTML 名称属性 @name = "URLString"

我对 c# 很陌生,我可能在这里遗漏了一些非常明显的东西。

4

1 回答 1

0

知道了。因为我的表单元素的名字一直有一个帖子。在名称之前我无法使用 URLString。我改为传递一个 post 对象并以这种方式获取 URL 字符串。我还通过 ID 进行更彻底的检查。

[HttpPost]
    public JsonResult doesURLExist(tPost post)
    {
        var allposts = _unitOfWork.PostRepository.Get();

        if (allposts.Count(p => p.URLString == post.URLString) == 0)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        if (allposts.Count(p => p.URLString == post.URLString && p.Id == post.Id) == 1)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        return Json(false, JsonRequestBehavior.AllowGet);
    }

这是我的模型,传递了一个附加字段“Id”,这也被放入我的帖子对象中。

            [RegularExpression("[a-z, 0-9]{1,50}", ErrorMessage = "URL String must be in lowercase")]
            [Required(ErrorMessage = "Unique URL is required")]
            [Remote("doesURLExist", "Post", AdditionalFields = "Id", HttpMethod = "POST", ErrorMessage = "URL already exists. Please enter a different URL.")]
            public string URLString { get; set; }

这是我的 HTML,现在一切正常。

<div class="form-group">
            @Html.LabelFor(model => model.post.URLString, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.post.URLString, new { htmlAttributes = new {  @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.post.URLString, "", new { @class = "text-danger" })
            </div>
        </div>
于 2015-02-10T12:32:37.883 回答