我对ASP.NET MVC3客户端验证有一个奇怪的问题.我有以下课程:
public class Instrument : BaseObject
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
[MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
从我的观点来看:
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
Run Code Online (Sandbox Code Playgroud)
这是我为此字段的文本框获取的生成的HTML:
<input class="text-box single-line" data-val="true" data-val-required="Name is required." id="Name" name="Name" type="text" value="">
Run Code Online (Sandbox Code Playgroud)
没有迹象MaxLengthAttribute,但其他一切似乎都在起作用.
有什么想法会出错吗?
我有以下viewmodel定义
public class AccessRequestViewModel
{
public Request Request { get; private set; }
public SelectList Buildings { get; private set; }
public List<Person> Persons { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
所以在我的应用程序中,访问请求必须至少有一个人.您可以使用什么方法进行验证?我不希望在我的控制器中进行这种验证,这很简单.是自定义验证属性的唯一选择吗?
编辑:目前正在使用FluentValidation执行此验证(漂亮的库!)
RuleFor(vm => vm.Persons)
.Must((vm, person) => person.Count > 0)
.WithMessage("At least one person is required");
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class CreateJob
{
[Required]
public int JobTypeId { get; set; }
public string RequestedBy { get; set; }
public JobTask[] TaskDescriptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想在上面有一个数据注释,TaskDescriptions以便数组必须包含至少一个元素?非常喜欢[Required].这可能吗?
我正在为这样的控制器编写单元测试:
public HttpResponseMessage PostLogin(LoginModel model)
{
if (!ModelState.IsValid)
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
Run Code Online (Sandbox Code Playgroud)
该模型看起来像:
public class LoginModel
{
[Required]
public string Username { set; get; }
[Required]
public string Password { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
然后我就像这样进行单元测试:
[TestMethod]
public void TestLogin_InvalidModel()
{
AccountController controller = CreateAccountController();
...
var response = controller.PostLogin(new LoginModel() { });
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}
Run Code Online (Sandbox Code Playgroud)
实际上ModelState已经过验证......这对我来说很奇怪,因为这两个字段都是必需的......有人可以建议吗?
c# unit-testing model-validation vs-unit-testing-framework asp.net-web-api
我有一张登记表,用户必须输入他们房子的面积.我希望这个值只是一个整数.有没有办法使用属性asp.net mvc验证此值?
我的ASP.NET MVC 3应用程序遇到了问题.我在我的模型中有2个属性,我只想在我的视图中根据其中任何一个为空而需要其中一个属性.例如,如果我输入电话号码,则不再需要电子邮件,反之亦然,但如果我将两者都留空,则应该需要1,下面是我的模型:
[Display(Name = "Contact Phone Number:")]
[MaxLength(150)]
public string ContactPhoneNumber { get; set; }
[Display(Name = "Contact Email Address:")]
[MaxLength(100)]
public string ContactEmailAddress { get; set; }
Run Code Online (Sandbox Code Playgroud)
我是否需要创建自定义属性来验证我的模型,如果是,我将如何实现这一目标?
我试图在MVC .Net Core中使用模型验证,并且无法管理替换此默认错误消息'值''无效'.
理论上,我们可以通过ErrorMessage在模型中使用Annotation 来替换我们自己的自定义错误消息.但我找不到让这个工作的方法.
我的模特
[Required(ErrorMessage = "Date Required")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]
[Display(Name = "Appointment Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime AppointmentDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
我把不同的ErrorMessage两个Required和DataType标签,如图上面.
我的HTML视图
<div class="col-md-2">
<input class="form-control" asp-for="AppointmentDate">
<span asp-validation-for="AppointmentDate" class="text-danger"></span>
</div>
Run Code Online (Sandbox Code Playgroud)
你能帮我解决一下如何取代错误信息吗?谢谢.
MVC 5是否有默认验证,我可以设置日期的最小值和最大值?
在我的模型中,我想要日期验证
public class MyClass
{
[Required(ErrorMessage="Start date and time cannot be empty")]
//validate:Must be greater than current date
[DataType(DataType.DateTime)]
public DateTime StartDateTime { get; set; }
[Required(ErrorMessage="End date and time cannot be empty")]
//validate:must be greater than StartDate
[DataType(DataType.DateTime)]
public DateTime EndDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Ps:根据这个Asp.Net网站,使用RangeDateTime验证器时出现问题,不推荐使用.
注意:jQuery验证不适用于Range属性和DateTime.例如,以下代码将始终显示客户端验证错误,即使日期在指定范围内:
[Range(typeof(DateTime), "1/1/1966", "1/1/2020")]您需要禁用jQuery日期验证才能将Range属性与DateTime一起使用.在模型中编译硬日期通常不是一个好习惯,因此不鼓励使用Range属性和DateTime.
我也知道有一些像Fluent Validation和Foolproof这样的Nuget软件包可以验证和检查一个日期是否比其他日期更大,但我想知道默认情况下是否有东西可以检查日期的最小值和最大值.
从我在MVC 5.1中的Whats new中看到的,有支持MaxLength和MinLength验证.
我知道在MVC中有很多方法可以进行模型验证,并且有很多关于这个主题的文档.不过,我不太清楚什么是用于验证的性能,最好的方法模型这是"子模型"的同一类型.
请记住以下内容
TryUpdateModel/TryValidateModel方法MainModel该类有一个强类型视图,用于呈现整个显示视图这可能听起来有点令人困惑但我会抛出一些代码来澄清.以下面的类为例:
MainModel:
class MainModel{
public SomeSubModel Prop1 { get; set; }
public SomeSubModel Prop2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
SomeSubModel:
class SomeSubModel{
public string Name { get; set; }
public string Foo { get; set; }
public int Number { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MainModelController:
class MainModelController{
public ActionResult MainDisplay(){
var main = db.retrieveMainModel();
return View(main);
}
[HttpGet]
public ActionResult EditProp1(){
//hypothetical retrieve …Run Code Online (Sandbox Code Playgroud) c# model-view-controller asp.net-mvc model-validation validationattribute
我将Azure blob url存储到我的数据库中.可以使用该URL获取blob吗?实际上我需要更新blob,在这样做时我需要验证.所以我需要将该数据库实体模型转换为我的本地模型并对它们应用验证.但在我的本地模型中我有Id,Name,HttpPostedFileBase文件.当我插入blob时,我得到blob url并保存它在database.But如何在更新时检索该blob?这是我的本地模特
public class BlobAppModel
{
public int Id { get; set; }
[Required(ErrorMessage="Please enter the name of the image")]
[Remote("IsNameAvailable","Home",HttpMethod="POST",ErrorMessage="Name Already Exists")]
public string Name { set; get; }
[Required(ErrorMessage="Please select an image file")]
public HttpPostedFileBase File { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
我的实体模型就是这个
public partial class BlobApp
{
public int Id { get; set; }
public string Name { get; set; }
public string Uri { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我编辑它时,我需要得到blob ..我被困在这里..任何人都可以帮助我吗?
public ActionResult Edit(string Id)
{ …Run Code Online (Sandbox Code Playgroud) model-validation ×10
c# ×8
asp.net-mvc ×7
.net ×1
asp.net ×1
blob ×1
int ×1
unit-testing ×1
validation ×1