0

我正在尝试使用视图模型和数据注释构建多个文件上传。以下是视图模型:

 public class UploadNewsModel
{
[File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
        public HttpPostedFileBase GenearlNews { get; set; }

        [File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
      public HttpPostedFileBase SportNews { get; set; }

        [File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
      public HttpPostedFileBase BusiNews { get; set; }

        [File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
      public HttpPostedFileBase InterNews { get; set; }

        [File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
      public HttpPostedFileBase EntertaintNews { get; set; }
        }

问题: 在这里,我想检查单个属性的模型状态以显示单个验证错误中的错误。我不知道要检查个人,而对于我们使用ModelState.IsValid的所有视图模型,我有以下操作:

  [HttpPost]
  public ActionResult Index(UploadNewsModel newsmodel)
{

    HttpPostedFileBase general = newsmodel.GenearlNews;
    HttpPostedFileBase sport = newsmodel.SportNews;
    HttpPostedFileBase business = newsmodel.BusiNews;
    HttpPostedFileBase international = newsmodel.InterNews;
    HttpPostedFileBase entertainment = newsmodel.EntertaintNews;


    if (general.ContentLength > 0 && general != null && ...check generalnews validation using data annotation == valid.. )
    {
        var fileName = Path.GetFileName(general.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads/News/General News/"), fileName);
        general.SaveAs(path);
    }
    else
    {
        .... add error of data annotation plus  below
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }

     ......... same for remaining upload file
    return View(newsmodel);
}

看法:

    @model IVRControlPanel.Models.UploadNewsModel

  @using (Html.BeginForm("index", "NewsUpload", FormMethod.Post, new { name = "form1", @id = "form1", enctype = "multipart/form-data" }))
        {


       @Html.ValidationSummary(true)
  @Html.TextBoxFor(model => model.GenearlNews, new {  type = "file" })
               @Html.ValidationMessageFor(model => model.GenearlNews)
          ........... same for remaining file upload
            }
4

2 回答 2

0

您的/UploadNewsModel没有任何属性,那么您究竟从哪里得到这些属性?UsernamePassword

如果您只想允许授权访问您的控制器操作,那么您应该使用AuthorizeAttribute

[HttpPost]  
[Authorize]
public ActionResult Index(UploadNewsModel newsmodel)
{
    ...
}
于 2012-08-21T11:48:54.427 回答
0

如果要返回错误消息,则需要检查 Isvalid:

  [HttpPost]
  public ActionResult Index(UploadNewsModel newsmodel)
{
    // If not Valid
    if (!ModelState.IsValid)
    {
       return this.View(newsmodel);
    }
    HttpPostedFileBase general = newsmodel.GenearlNews;
    HttpPostedFileBase sport = newsmodel.SportNews;
    HttpPostedFileBase business = newsmodel.BusiNews;
    HttpPostedFileBase international = newsmodel.InterNews;
    HttpPostedFileBase entertainment = newsmodel.EntertaintNews;


    if (general.ContentLength > 0 && general != null && ...check generalnews validation using data annotation == valid.. )
    {
        var fileName = Path.GetFileName(general.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads/News/General News/"), fileName);
        general.SaveAs(path);
    }
    else
    {
        .... add error of data annotation plus  below
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }

     ......... same for remaining upload file
    return View(newsmodel);
}

这样它应该只显示发布文件的错误;如果要填写所有 5 个字段,则需要添加 [必填]:

[Required]
     [File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
          public HttpPostedFileBase SportNews { get; set; }

** 您将无法单独检查每个属性以进行验证,但使用 isValid 您将通知用户文件不正确。

于 2012-08-21T11:47:09.797 回答