FormHelper

其他类别 2025-08-20

形成助手

#1 ASP.NET核心MVC的表格库

如果您喜欢这个图书馆并想支持它,请给星星。

ASP.NET核心MVC的形式和验证助手

形式的帮助者可以帮助您在不编写任何JavaScript代码的情况下创建AJAX表单和验证。它将服务器端验证转换为客户端。您也可以使用无ajax的表单验证器。

与流利验证兼容

(您可以将客户端验证支持添加到Fluent验证中。)

安装

可以使用Nuget软件包管理器Dotnet CLI安装FormHelper 。

软件包管理器:

 Install-Package FormHelper

dotnet cli:

 dotnet add package FormHelper

该图书馆与jQuery一起使用

CDN:

FormHelper .min.js/css to debug. --> ">
 




< link rel =" stylesheet " href =" / FormHelper / FormHelper .min.css " />
< script src =" / FormHelper / FormHelper .bundle.min.js " >  script > 

用法

startup.cs

配置服务:

 services.AddControllersWithViews().Add FormHelper ();

使用配置:(可选)

FormHelper(options => { options.CheckTheFormFieldsMessage = "Your custom message..."; options.RedirectDelay = 6000; options.EmbeddedFiles = true; options.ToastrDefaultPosition = ToastrPosition.TopFullWidth; });">
 services.AddControllersWithViews().Add FormHelper (options => {
    options.CheckTheFormFieldsMessage = "Your custom message...";
    options.RedirectDelay = 6000;
    options.EmbeddedFiles = true;
    options.ToastrDefaultPosition = ToastrPosition.TopFullWidth;
});

配置:

FormHelper();">
 
app.Use FormHelper ();

viewimports.cshtml

FormHelper @addTagHelper *, FormHelper ">
 @using FormHelper
@addTagHelper * , FormHelper

查看:( taghelper)

FormHelper="true" asp-controller="Home" asp-action="Save"> // // You can use
or < FormHelper > to activate FormHelper . // Optional parameters: // asp-callback="javascriptFunctionName", asp-beforeSubmit="javascriptFunctionName", asp-dataType="FormData/Json", asp-enableButtonAfterSuccess="false", asp-resetFormAfterSuccess="true" asp-toastrPosition="ToastrPosition.BottomRight"">
 < form asp - FormHelper = "true" asp - controller = "Home" asp - action = "Save" >
   // 
   // ...
< / form >

// You can use  or < FormHelper > to activate FormHelper .
// Optional parameters:
// asp-callback="javascriptFunctionName", asp-beforeSubmit="javascriptFunctionName", asp-dataType="FormData/Json", asp-enableButtonAfterSuccess="false", asp-resetFormAfterSuccess="true" asp-toastrPosition="ToastrPosition.BottomRight"

控制器:

 [ FormValidator ]
public IActionResult Save ( FormViewModel viewModel )

// If you use Json data type, you need to add [FromBody] attribute.
// public IActionResult Save([FromBody]FormViewModel viewModel)

返回控制器的结果:

错误信息:

 return FormResult.CreateErrorResult("An error occured.");

成功消息:

 return FormResult.CreateSuccessResult("Product saved.");

与重定向的成功消息:

 return FormResult.CreateSuccessResult("Product saved. Please wait...", Url.Action("Home", "Index"));

重定向和延迟时间的成功消息:

 return FormResult.CreateSuccessResult("Product saved. Please wait...", Url.Action("Home", "Index"), 10000); // 10 seconds

从JSON对象填充表单字段:

 $("#formId").fillFormFields(yourJsonObject);

重置表单和清晰的错误消息:

 $("#formId").fhReset();

toastr:

成功:

 fhToastr.success("Text here");

警告:

 fhToastr.warning("Text here");

信息:

 fhToastr.information("Text here");

错误:

 fhToastr.error("Text here");
下载源码

通过命令行克隆项目:

git clone https://github.com/sinanbozkus/FormHelper.git