|
Re: Localization
#37723225
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59.
public class NewCommand
{
public int Command_id { get; set; }
[FileSize(MaxContentLength = 1 * 1024 * 1024)]
public HttpPostedFileBase File { get; set; }
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class FileSizeAttribute : DataTypeAttribute, IClientValidatable
{
private int _maxContentLength = 1 * 1024 * 1024;
public FileSizeAttribute()
: base("upload")
{
ErrorMessage = string.Format("File is too large, maximum allowed is: {0} MB", MaxContentLength * 1.0 / (1024 * 1024));
}
public int MaxContentLength
{
get
{
return _maxContentLength;
}
set
{
_maxContentLength = value;
}
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ValidationType = "filesize",
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
};
rule.ValidationParameters["max"] = _maxContentLength;
yield return rule;
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
HttpPostedFileBase valueAsFileBase = value as HttpPostedFileBase;
if (valueAsFileBase.ContentLength > MaxContentLength)
{
ErrorMessage = string.Format("File is too large, maximum allowed is: {0} MB", MaxContentLength * 1.0 / (1024 * 1024));
}
return false;
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
<script type="text/javascript">
(function ($) {
$.validator.addMethod("filesize", function (value, element, params) {
return 100 > 99;
});
$.validator.unobtrusive.adapters.add("filesize", ["max"], function (options) {
options.rules['filesize'] = options.params.max;
options.messages['filesize'] = options.message;
});
} (jQuery));
</script>
1. 2. 3. 4. 5. 6.
<p id="pUpload" style="display:block;">
@Html.LabelFor(model => Model.New.File, @Html.Resource("Strings, MaxFileSize"))
@Html.TextBoxFor(model => Model.New.File, new { type = "file", @style = "border: solid 1px black; font: bold 12px Verdana; width:95%" })
@Html.ValidationMessageFor(model => Model.New.File)
</p>
Валидация на клиенте не запускается. В чем мб ошибка не могу понять. :-(
|
|
|