加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:36:27 所属栏目:百科 来源:网络整理
导读:我正在寻找一种方法来验证ASP View页面上的两个字段.我知道验证表单字段的常用方法是在页面上包含一些@model ViewModel对象,其中此模型对象的属性将使用验证所需的适当注释进行注释.例如,注释可以是这样的: [Required(ErrorMessage = "Please add the messa
我正在寻找一种方法来验证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",@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对象的情况下实现这种验证,以及如何做到这一点?谢谢.

解决方法

如果您正在寻找服务器端验证,您可以使用以下内容
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",@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",@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",@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>
}

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读