【问题标题】:mvc server side validation helpmvc 服务器端验证帮助
【发布时间】:2011-09-09 15:52:45
【问题描述】:

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

我正在尝试对 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,我想重定向到相同的默认页面,可能会出现一条消息:&lt;%= Html.ValidationMessage("url", "*") %&gt; 但是我不知道该怎么做。 在此先感谢 Laziale

编辑:

在完成您推荐的所有更改后,我在查看页面的标题中收到错误消息。我有 Inherits="ViewPageBase" 其中 Home 是类的名称,Home.cs 在 Models 文件夹中。 在 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 指令或程序集引用?)”

知道我错过了什么吗? 谢谢,Laziale

【问题讨论】:

  • 你有HomeController吗?

标签: asp.net-mvc asp.net-mvc-2 html-helper asp.net-mvc-validation


【解决方案1】:
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");
}

或者您可以使用DataAnnotationsModel 和强类型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>
<% } %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 2014-08-11
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多