C# ASP.NETMVC验证模型
我在确认表格时遇到了一些困难 既然我想从数据库中填充combobox(另一个表,我正在使用viewbags来完成此操作),那么在本例中是否有方法使用ComboBoxFor,以便使用System.ComponentModel.DataAnnotations和jquery.validate.js 视图: 编辑: 以下是我目前对这一点的实施情况: Layout.cshtmlC# ASP.NETMVC验证模型,c#,asp.net-mvc,validation,C#,Asp.net Mvc,Validation,我在确认表格时遇到了一些困难 既然我想从数据库中填充combobox(另一个表,我正在使用viewbags来完成此操作),那么在本例中是否有方法使用ComboBoxFor,以便使用System.ComponentModel.DataAnnotations和jquery.validate.js 视图: 编辑: 以下是我目前对这一点的实施情况: Layout.cshtml public partial class Document { private int _iD; public
public partial class Document
{
private int _iD;
public virtual int ID
{
get
{
return this._iD;
}
set
{
this._iD = value;
}
}
private string _fileType;
[Required(ErrorMessage = "Required!")]
public virtual string FileType
{
get
{
return this._fileType;
}
set
{
this._fileType = value;
}
}
}
请记住添加:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
型号
public partial class Document
{
private int _iD;
public virtual int ID
{
get
{
return this._iD;
}
set
{
this._iD = value;
}
}
private string _fileType;
[Required(ErrorMessage = "Required!")]
public virtual string FileType
{
get
{
return this._fileType;
}
set
{
this._fileType = value;
}
}
}
是否有充分的理由使用Viewmodel over Viewbag来填充DropDownListFors(从中找到)
这个实现的唯一问题是我无法在客户端验证文件(所以用户不能发布空文件)
编辑:只需添加以下内容即可实现此功能:
<input type="file" data-val="true" data-val-required="Please select a file!" name="file" />
通常情况下,您有3个不同的部分,用于构建场景的正确实现 ViewModel: 在正确的实现中,每个视图都有自己的Viewmodel。在您的情况下,可能是这样的:
public class CreateDocumentViewModel
{
[Required]
public IEnumerable<SelectListItem> filetypes { get; set; }
// Maybe some more attributes you need in the view?
}
最后,您需要保存回db的POST操作
[HttpPost]
public ActionResult CreateDocument(HttpPostedFileBase file, string fileType)
{
if (file != null && file.ContentLength > 0)
{
Document doc = new Document()
{
Filetype = fileType
}
if (this.TryValidateModel(doc))
{
this.dbContext.Add(doc);
this.dbContext.SaveChanges();
id = doc.ID;
//save document using document ID
}
}
}
我希望我完全理解了您的问题,这有助于解决问题。如果您没有模型,并且在模型的属性中添加了验证属性,那么您将无法立即获得验证。既然您在视图中声明了
@model Document
,那么Document
需要有一个属性文件类型
,或者使用视图模型来显示和编辑您想要查找的内容@Html.DropDownListFor?您可以将其用于模型的属性,然后使用数据批注对其进行验证。@Stephen,我已将验证属性指定给模型属性。@Muthu,是的,我是指DropDownListFor,但我遇到了一个问题:如何从Viewbag填充DropDownListFor?您需要发布您的模型。如果您有一个名为fileType
的属性,那么应该没有问题。感谢您提供清晰的示例。所以我应该把我的模型(MyProject.OpenAccess.Document)和那些dropdownlists包含在同一个viewmodel中?在您的示例中,似乎您正在验证filetypes集合(DropDownList数据),而在发回表单时没有进行验证,我错了吗?是的,这就是想法。发布表单后,将立即验证viewmodel中的数据批注。例如,如果您有一个viewmodel和一个属性“password”,内部用“required”修饰,则在您填写视图中的password字段之前,您无法发布表单。
<input type="file" data-val="true" data-val-required="Please select a file!" name="file" />
public class CreateDocumentViewModel
{
[Required]
public IEnumerable<SelectListItem> filetypes { get; set; }
// Maybe some more attributes you need in the view?
}
@model CreateDocumentViewModel
@using (Html.BeginForm("CreateDocument", "Create", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label>Select file:</label><br />
<input type="file" name="file" /><br />
<label>Filetype</label><br />
@Html.DropDownListFor(model => model.filetypes, model.filetypes, "-Filetypes", new { @class = "filetype-cb" })
<br />
<input type="submit" value="Add"/>
}
[HttpGet]
public ActionResult CreateDocument()
{
CreateDocumentViewModel model = new CreateDocumentViewModel();
model.filetypes = FillFileTypesFromDB; // Here you fill the data for the dropdown!
return View(model);
}
[HttpPost]
public ActionResult CreateDocument(HttpPostedFileBase file, string fileType)
{
if (file != null && file.ContentLength > 0)
{
Document doc = new Document()
{
Filetype = fileType
}
if (this.TryValidateModel(doc))
{
this.dbContext.Add(doc);
this.dbContext.SaveChanges();
id = doc.ID;
//save document using document ID
}
}
}