【问题标题】:Html.beginform validation Server and Client sideHtml.beginform 验证服务器端和客户端
【发布时间】:2016-09-15 19:53:02
【问题描述】:

处理我的第一个 ASP.NET MVC 应用程序并遇到一些表单验证问题。

我有我的模型:

public class InfoFormEmplModel
{
    public int supID { get; set; }
    public string description { get; set; }

    public InfoFormEmplModel() {}

}

请注意,此模型不代表我数据库中的任何表。

现在,在我看来:

@using Portal.Models
@model InfoFormEmplModel
@{
    ViewBag.Title = "Form";
 }


@using (Html.BeginForm())
{
    <b>Sup</b> @Html.TextBoxFor(x => x.supID)

    <p>Description</p>
    @Html.TextAreaFor(x => x.description)<br><br>

    <input type="submit" name="Save" value="Soumettre" />
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我需要进行一些验证,字段不能为空,并且我还必须检查提供的 supId 是否存在于我的数据库中(服务器端验证)

我尝试为我的模型添加一些验证:

public class InfoFormEmplModel
    {
        [Required (ErrorMessage = "Superior ID required")]
        public int supID { get; set; }

        [Required (ErrorMessage = "Description required")]
        public string description { get; set; }

        public InfoFormEmplModel() {}        
    }

并且还在我的视图中添加了@Html.ValidationMessageFor:

@using Portal.Models
    @model InfoFormEmplModel
    @{
        ViewBag.Title = "Form";
     }


    @using (Html.BeginForm())
    {
        <b>Sup</b> @Html.TextBoxFor(x => x.supID)
        @Html.ValidationMessageFor(x => x.supID)

        <p>Description</p>
        @Html.TextAreaFor(x => x.description)<br><br>
        @Html.ValidationMessageFor(x => x.description)

        <input type="submit" name="Save" value="Soumettre" />
    }

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

我的控制器如下所示:

[HttpPost]
public PartialViewResult invform(InfoFormEmplModel form)
    {

        //check if supID exists
        bool exists = librairie.supExists(form.supID);
        if (!exists)
        {
            return PartialView("ErreurDuplicat");
        }

        return PartialView("Success");
    }

当我将 supID 留空时,验证似乎不会发生。我的控制器将我的模型发送到另一个类,该类检查 superieur Id 是否在数据库中,但 supID 没有任何值。我期待在控制器继续之前,我会在网页上看到错误消息..

另外,一旦我检查了数据库中是否存在 supID,如何在我的视图中显示错误消息以便用户可以输入有效的 supID?

【问题讨论】:

  • 您在视图中使用的模型与您应用验证的模型不同。应该是@model InfoFormulaireEmployeModele
  • 糟糕,我更正了。我对代码进行了一些更改,使其更小并且没有一堆法语变量
  • 仍然,您确定这是您使用的代码吗?您的属性在视图和模型中的名称不同。您只需将代码添加到您当前的模型、视图和控制器中以避免任何混淆。
  • 您是否还包括jquery{version}.js

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


【解决方案1】:

假设您始终使用相同的视图模型(并且为了清楚起见,您进行了翻译和缩短),您应该在您的后期操作中获得您的视图模型。然后您可以使用 ModelState 属性根据您的验证注释检查接收到的模型是否有效。

如果你的模型是有效的,你可以在服务器端检查 SupId,如果你想设置一个错误,如果这样的 Id 已经存在,你可以像下面的 sn-p 那样做:

[HttpPost]
    public ActionResult invform(InfoFormEmplModel form)
    {
        if (ModelState.IsValid)
        {
            //set an error when the id exists    
            ModelState.AddModelError("supId", "The Id is already in use. Please chose a different Id");
            return View(form);
        }

        return View(form);
    }

对于另一个错误是不可能的,因为它是一个 int。那么也许您还缺少其他东西?

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2011-10-17
    • 2023-04-08
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多