开发者

Validation not working using Data annotation in mvc

I Have created view model with the validation in it, but the validation doesnt works when i submit the form, below is the code :

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; }
}

}

View

<%@ 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.acc开发者_如何转开发omodation.ProductWebsite)%>
        </div>
        </td></tr> </table>

Controller

[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);

    }


You should use the metadata class to apply validate to existing model fields/properties thus:

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; }
    }

}

--EDIT--

Looking at you code further it seems you are trying to apply validation to fields in the Accommodation class, not the SampleFormViewModel. You appear to only have a single Accommodation instance so your view model should probably not be passing the Accommodation object to the View but should extract the fields and present those as the view model alternatively (and I'm not sure if this would work) you should apply you metadata class to the Accommodation object, not the SampleFormViewModel.


Include jqueryvalidate in your .cshtml file. For example, if the script has been bundled up in your BundleConfig file, then your code will look like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜