验证在mvc中使用数据注释无效

时间:2011-07-13 11:42:45

标签: asp.net-mvc asp.net-mvc-2 validation viewmodel data-annotations

我已经创建了带有验证的视图模型,但是当我提交表单时验证不起作用,下面是代码:

namespace Products.Models
{
[MetadataType(typeof(SampleFormViewModelMetaData))]
public partial class SampleFormViewModel
{
    public SampleFormViewModel() { }

    public Venue venues { get; set; }
    public Accomodation accomodation { get; set; }
}

public class SampleFormViewModelMetaData
{


    [Required(ErrorMessage = "*")]
    public object ProductName { get; set; }

    [Required(ErrorMessage = "*")]
    public object ProductDescription { get; set; }

    [Required(ErrorMessage = "*")]
    public object ProductWebsite { get; set; }

    [Required(ErrorMessage = "Tel required")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public object ProductTel { get; set; }
}

}

查看

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ApexAdmin.Master" Inherits="System.Web.Mvc.ViewPage<ApexTrackDays.Models.SampleFormViewModel>" %>
            <div class="editor-label">
            <%: Html.LabelFor(model => model.accomodation.ProductName)%>
        </div>
        </td>
        <td>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.accomodation.ProductName, new { style = "width:300px;" })%>
            <%: Html.ValidationMessageFor(model => model.accomodation.ProductName)%>
        </div>
        </td>
        </tr>

        <tr><td colspan="2">
        <div class="editor-label">
            <%: Html.LabelFor(model => model.accomodation.ProductDescription)%>
        </div>
        </td>
        </tr><tr><td colspan="2">
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.accomodation.ProductDescription, new { @class = "tinymce" })%>
            <%: Html.ValidationMessageFor(model => model.accomodation.ProductDescription)%>
        </div>

       </td></tr>

        <tr><td>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.accomodation.ProductWebsite)%>
        </div>
        </td><td>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.accomodation.ProductWebsite, new { style = "width:300px;" })%>
            <%: Html.ValidationMessageFor(model => model.accomodation.ProductWebsite)%>
        </div>
        </td></tr> </table>

控制器

[HttpPost]
    public ActionResult Create(FormCollection formValues)
    {
       // ApextrackdaysEntities entity = new ApextrackdaysEntities();
        IAccomodationTypeRepository AccomodationResp = new AccomodationTypeRepository();
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
        Accomodation accomodation = new Accomodation();
        if (TryUpdateModel(accomodation))
        {
            accomodation.DateAdded = DateTime.Now;
            accomodation.DateModified = DateTime.Now;
            accomResp.Add(accomodation);
            accomResp.Save();
            int AccomodationID = accomodation.ID;
            int VenueID = Convert.ToInt16(formValues["Venue"]);
            AccomodationType type = new AccomodationType();
            type.AccomodationID = AccomodationID;
            type.TrackID = VenueID;

            AccomodationResp.Add(type);
            AccomodationResp.Save();


            return RedirectToAction("Index", new { id = accomodation.ID });
        }
        return View(accomodation);

    }

2 个答案:

答案 0 :(得分:2)

您应该使用元数据类将验证应用于现有模型字段/属性:

namespace Products.Models
{

    [MetadataType(typeof(SampleFormViewModelMetaData))]
    public partial class SampleFormViewModel
    {
        public SampleFormViewModel() { }

        public Venue venues { get; set; }
        public Accomodation accomodation { get; set; }

        public object ProductName { get; set; }
        public object ProductDescription { get; set; }
        public object ProductWebsite { get; set; }
        public object ProductTel { get; set; }
    }

    public class SampleFormViewModelMetaData
    {


        [Required(ErrorMessage = "*")]
        public object ProductName { get; set; }

        [Required(ErrorMessage = "*")]
        public object ProductDescription { get; set; }

        [Required(ErrorMessage = "*")]
        public object ProductWebsite { get; set; }

        [Required(ErrorMessage = "Tel required")]
        [DisplayFormat(ConvertEmptyStringToNull = false)]
        public object ProductTel { get; set; }
    }

}

- 编辑 -

进一步查看代码,您似乎正在尝试将验证应用于Accommodation类中的字段,而不是SampleFormViewModel。您似乎只有一个Accommodation实例,因此您的视图模型可能不应该将Accommodation对象传递给View但是应该提取字段并将它们作为视图模型交替显示(并且我不确定这是否有效)应该将元数据类应用于Accommodation对象,而不是SampleFormViewModel。

答案 1 :(得分:1)

在.cshtml文件中包含jqueryvalidate。例如,如果脚本已捆绑在BundleConfig文件中,那么您的代码将如下所示

@section scripts
 {
     @Scripts.Render("~/bundles/jqueryval")
 }