在 MVC 中提交表单之前更改模型
Change model before Form Submit in MVC
我创建了以下表格:
@using (Html.BeginForm("Export", "ExportDefinitions", FormMethod.Post, new { ExportModel = Model }))
{
<input name="button" type="submit" value="Export" />
}
此表单上方是一个对象,允许选择多个 ID。这个也需要提交。
C# "ExportDefinitions" 定义为:
public string SelectedIds { get; set; }
public bool OnlyForSelected { get; set; }
在提交表单之前是否可以更改 "Model" 的内容?
我有一些检查需要先完成,这将在需要时修改所需的结果。
MVC 新手,如有任何帮助,我们将不胜感激。
您可以在模型的属性中使用最小长度、必需等属性
像这样
[Required]
public string SelectedIds { get; set; }
public bool OnlyForSelected { get; set; }
然后您可以 ModelState.IsValid 在发帖前检查您的视图。但是恕我直言,使用 jquery 进行表单验证要容易得多,效率也更高。
您可以随时使用 System.ComponentModel.DataAnnotations,
可以在此处找到关于此的基本教程,basic tutorial abount DataAnnotations;
在您看来使用智能代码进行验证是一种不好的做法。
我创建了以下表格:
@using (Html.BeginForm("Export", "ExportDefinitions", FormMethod.Post, new { ExportModel = Model }))
{
<input name="button" type="submit" value="Export" />
}
此表单上方是一个对象,允许选择多个 ID。这个也需要提交。
C# "ExportDefinitions" 定义为:
public string SelectedIds { get; set; }
public bool OnlyForSelected { get; set; }
在提交表单之前是否可以更改 "Model" 的内容?
我有一些检查需要先完成,这将在需要时修改所需的结果。
MVC 新手,如有任何帮助,我们将不胜感激。
您可以在模型的属性中使用最小长度、必需等属性 像这样
[Required]
public string SelectedIds { get; set; }
public bool OnlyForSelected { get; set; }
然后您可以 ModelState.IsValid 在发帖前检查您的视图。但是恕我直言,使用 jquery 进行表单验证要容易得多,效率也更高。
您可以随时使用 System.ComponentModel.DataAnnotations, 可以在此处找到关于此的基本教程,basic tutorial abount DataAnnotations;
在您看来使用智能代码进行验证是一种不好的做法。