如何在MVC中使用Annotations [英] How do I use Annotations in MVC

查看:70
本文介绍了如何在MVC中使用Annotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视野中的代码是......



输入您的名字:@ Html.TextBoxFor(f => f.F_Name)



@ Html.ValidationMessageFor(m => m.F_Name)



输入姓氏:@ Html.TextBoxFor(l => ; l.L_Name)



@ Html.ValidationMessageFor(m => m.L_Name)



输入薪资:@ Html.TextBoxFor(s => s.sal)



@ Html.ValidationMessageFor(m => m.sal)



在模型中我写了这些代码行:



my code in view is ...

Enter Your First Name: @Html.TextBoxFor(f => f.F_Name)


@Html.ValidationMessageFor(m=>m.F_Name)

Enter Last Name: @Html.TextBoxFor(l => l.L_Name)


@Html.ValidationMessageFor(m=>m.L_Name)

Enter Salary: @Html.TextBoxFor(s => s.sal)


@Html.ValidationMessageFor(m=>m.sal)

and in model i have written these lines of code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using ProjectTemplate.Web.Infrastructure;

namespace ProjectTemplate.Web.Models
{
    public class validation
    {
        [Required(ErrorMessage = "The First Name field is required")]
        [Display(Name="Enter Your First Name:")]
         public  string F_Name { get; set; }

        [Required(ErrorMessage = "Last Name field is required")]
         public   string L_Name { get; set; }

        [Range(9000,12000,ErrorMessage="Salary Amount should be between 9k to 12k")]
        [DisplayName("Enter Your First Name:")]
         public int sal { get; set; }


    }
}

but still its not working ..... :( please someone help me ... thank u in adv.

推荐答案

您是否检查控制器中的数据是否有效?检查此链接并查看 PersonController 中的创建方法:

< a href =https://msdn.microsoft.com/en-us/library/dd410405%28v=vs.100%29.aspx> https://msdn.microsoft.com/en-us/library/dd410405 %28v = vs.100%29.aspx [ ^ ]



祝你好运!
Do you check if the data is valid in your controller? Check this link and have a look at the Create method in PersonController:
https://msdn.microsoft.com/en-us/library/dd410405%28v=vs.100%29.aspx[^]

Good luck!


这篇关于如何在MVC中使用Annotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆