0

我有一个问题,我相信这里有人肯定会知道答案。

我正在尝试对 MVC 网站中的文本框进行服务器端验证。这是我所拥有的:

   <% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get))
       {%>   
    <%: Html.ValidationSummary("Please enter valid URL and try again.") %>
    <fieldset>
    <p>
    <%=Html.Label("Please enter URL:") %>
    <%=Html.TextBox("url")%>
    <%= Html.ValidationMessage("url", "*") %>

    <input type="submit" value="Crawl" />

    </p>
    </fieldset>
    <% } %>

在控制器中我有这个:

public ActionResult WebsiteLinks(string url)
        {
            if (Regex.IsMatch(url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"))
            {
                ViewData["AnchorText"] = url;
                return View(new Website(url, "Url"));
            }
            return RedirectToAction("Index");
        }

验证工作正常,但我想要实现的是如果数据无效,如果数据不是正确的 url,我想重定向到相同的默认页面,可能会出现一条消息: <%= Html.ValidationMessage("url", "*") %>但我不知道如何要做到这一点。提前致谢, Laziale

编辑:

在完成您推荐的所有更改后,我在视图页面的标题中收到错误消息。我有 Inherits="ViewPageBase" ,其中 Home 是 Models 文件夹中的类名 Home.cs。在 home.cs 文件中,我有这个:

namespace LAX.Models
{
    public class UrlModel
    {
        [Required]
        [DisplayName("Please enter URL:")]
        [RegularExpression(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?")]
        public string Url { get; set; }
    }
}

在控制器中我有:

   [HttpPost]
        public ActionResult WebsiteLinks(UrlModel model)
        {
            /*
            if (Regex.IsMatch(url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"))
            {
                ViewData["AnchorText"] = url;
                return View(new Website(url, "Url"));
            }
            else
            {
                ModelState.AddModelError("url", "Error URL Format");
            }
            return RedirectToAction("Index");
             */

            if (ModelState.IsValid)
            {
                ViewData["AnchorText"] = model.Url;
                return View(new Website(model.Url, "Url"));
            }
            return RedirectToAction("Index");

        }

在我看来,我有:

  <% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get))
       {%>   
    <%: Html.ValidationSummary("Please enter valid URL and try again.") %>
    <fieldset>
    <p>
        <%=Html.LabelFor(m => m.Url) %>
        <%=Html.TextBoxFor(m => m.Url) %>
        <%=Html.ValidationMessageFor(m => m.Url) %>

    <input type="submit" value="Crawl" />

    </p>
    </fieldset>
    <% } %>

这是错误:“找不到类型或命名空间名称'Home'(您是否缺少 using 指令或程序集引用?)”

知道我错过了什么吗?谢谢,拉齐尔

4

1 回答 1

0
public ActionResult WebsiteLinks(string url)
{
    if (Regex.IsMatch(url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"))
    {
        ViewData["AnchorText"] = url;
        return View(new Website(url, "Url"));
    }
    else
    {    
        ModelState.AddModelError("url", "*");
    }

    return RedirectToAction("Index");
}

DataAnnotations或者你可以用, Model, 和强类型使这个更性感View

模型:

public class UrlModel
{
    [Required]
    [DisplayName("Please enter URL:")]
    [RegularExpression(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?")]
    public string Url { get; set; }
}

控制器:

public ActionResult WebsiteLinks(UrlModel model)
{
    if (ModelState.IsValid)
    {
        ViewData["AnchorText"] = model.Url;
        return View(new Website(model.Url, "Url"));
    }
    return RedirectToAction("Index");
}

看法:

<%@ Page Language="C#" Inherits="ViewPageBase<UrlModel>" %>
<% using (Html.BeginForm("WebsiteLinks", "Home", FormMethod.Get)) {%>   
    <%: Html.ValidationSummary("Please enter valid URL and try again.") %>
    <fieldset>
    <p>
        <%=Html.LabelFor(m => m.Url) %>
        <%=Html.TextBoxFor(m => m.Url) %>
        <%=Html.ValidationMessageFor(m => m.Url) %>

        <input type="submit" value="Crawl" />
    </p>
</fieldset>
<% } %>
于 2011-09-09T15:58:23.980 回答