ASP .NET MVC表单字段验证(无模型)

时间:2014-09-15 08:07:45

标签: c# asp.net asp.net-mvc asp.net-mvc-4 validation

我正在寻找一种在ASP View页面上验证两个字段的方法。我知道验证表单字段的常用方法是在页面上包含一些@model ViewModel对象,其中此模型对象的属性将使用验证所需的适当注释进行注释。例如,注释可以是这样的:

[Required(ErrorMessage = "Please add the message")]
[Display(Name = "Message")]

但是,在我的情况下,页面上没有包含模型,并且从表单调用的控制器操作接收平面字符串作为方法参数。 这是表单代码:

@using (Html.BeginForm("InsertRssFeed", "Rss", FormMethod.Post, new { @id = "insertForm", @name = "insertForm" }))
{
<!-- inside this div goes entire form for adding rssFeed, or for editing it -->
    ...
            <div class="form-group">
                <label class="col-sm-2 control-label"> Name:</label>
                <div class="col-sm-10">
                    <div class="controls">
                        @Html.Editor("Name", new { htmlAttributes = new { @class = "form-control", @id = "add_rssFeed_name" } })
                    </div>
                </div>
            </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label"> URL:</label>
                    <div class="col-sm-10">
                        <div class="controls">
                            @Html.Editor("Url", new { htmlAttributes = new { @class = "form-control", @id = "add_rssFeed_Url" } })
                        </div>
                    </div>
                </div>

            </div>
        </div>
            <!-- ok and cancel buttons. they use two css classes.  button-styleCancel is grey button and button-styleOK is normal orange button -->
        <div class="modal-footer">
            <button type="button" class="button-styleCancel" data-dismiss="modal">Close</button>
            <button type="submit" class="button-styleOK" id="submitRssFeed">Save RSS Feed</button>
        </div>
}

您可以看到该表单正在向RssController操作方法发送两个文本字段(Name和Url),该方法接受这两个字符串参数:

[HttpPost]
public ActionResult InsertRssFeed(string Name, string Url)
{

    if (!String.IsNullOrEmpty(Name.Trim()) & !String.IsNullOrEmpty(Url.Trim()))
    {
        var rssFeed = new RssFeed();
        rssFeed.Name = Name;
        rssFeed.Url = Url;

        using (AuthenticationManager authenticationManager = new AuthenticationManager(User))
        {
            string userSid = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.PrimarySid);
            string userUPN = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.Upn);

            rssFeedService.CreateRssFeed(rssFeed);
        }
    }

    return RedirectToAction("ReadAllRssFeeds", "Rss");
}

如果页面有模型,验证将使用@Html.ValidationSummary方法完成,但正如我所说,我没有在页面上使用modelview对象。 有没有办法在不使用ModelView对象的情况下实现这种验证,以及如何做到这一点?感谢。

1 个答案:

答案 0 :(得分:6)

如果您正在寻找服务器端验证,可以使用

之类的内容
ModelState.AddModelError("", "Name and Url are required fields.");

但你需要添加

@Html.ValidationSummary(false)

到Html.BeginForm部分内的剃刀视图,然后代码如下所示。

[HttpPost]
public ActionResult InsertRssFeed(string Name, string Url)
{

    if (String.IsNullOrEmpty(Name.Trim()) || String.IsNullOrEmpty(Url.Trim()))
    {
        ModelState.AddModelError("", "Name and URL are required fields.");
        return View();
    }

    var rssFeed = new RssFeed();
        rssFeed.Name = Name;
        rssFeed.Url = Url;

    using (AuthenticationManager authenticationManager = new AuthenticationManager(User))
    {
        string userSid = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.PrimarySid);
        string userUPN = authenticationManager.GetUserClaim(SystemClaims.ClaimTypes.Upn);

        rssFeedService.CreateRssFeed(rssFeed);
    }

    return RedirectToAction("ReadAllRssFeeds", "Rss");
}

如果您只寻找客户端验证,那么您必须使用Jquery等客户端验证库。

http://runnable.com/UZJ24Io3XEw2AABU/how-to-validate-forms-in-jquery-for-validation

编辑评论部分

你的剃须刀应该是这样的。

@using (Html.BeginForm("InsertRssFeed", "Rss", FormMethod.Post, new { @id = "insertForm", @name = "insertForm" }))
{
    @Html.ValidationSummary(false)

        <div class="form-group">
            <label class="col-sm-2 control-label"> Name:</label>
            <div class="col-sm-10">
                <div class="controls">
                    @Html.Editor("Name", new { htmlAttributes = new { @class = "form-control", @id = "add_rssFeed_name" } })
                </div>
            </div>
        </div>

            <div class="form-group">
                <label class="col-sm-2 control-label"> URL:</label>
                <div class="col-sm-10">
                    <div class="controls">
                        @Html.Editor("Url", new { htmlAttributes = new { @class = "form-control", @id = "add_rssFeed_Url" } })
                    </div>
                </div>
            </div>

        </div>
    </div>
        <!-- ok and cancel buttons. they use two css classes.  button-styleCancel is grey button and button-styleOK is normal orange button -->
    <div class="modal-footer">
        <button type="button" class="button-styleCancel" data-dismiss="modal">Close</button>
        <button type="submit" class="button-styleOK" id="submitRssFeed">Save RSS Feed</button>
    </div>
}

希望这有帮助。