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

本文关键字:模型 验证 字段 NET MVC 表单 ASP | 更新日期: 2023-09-27 18:21:49

我正在寻找一种方法来验证ASP视图页面上的两个字段。我知道,验证表单字段的常用方法是在页面上包含一些@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对象的情况下实现这种验证,以及如何做到这一点?谢谢

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

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

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>
}

希望这能有所帮助。