0

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.accomodation.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);

    }
3
  • And what does your controller action that accepts the post back look like? Commented Jul 13, 2011 at 11:47
  • it returns that the model is valid , which shows d validation is not done Commented Jul 13, 2011 at 11:50
  • I have added the post controller Commented Jul 13, 2011 at 11:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

That's because you are providing Model.accommodation.ProductName with an editor and not Model.ProductName. Read my edit above.
But I want to use multiple models in a single view thats why i create a viewmodel and pass two entity model which is venue and accomodation, as i will be using Model.venue.name later on in the view. I want to use the properties of both the table in the view and do validation check , hope it make sense
I see what you are trying to do but you simply aren't understanding how validation works. It's specific, the validation MetaData class has field definitions that map exactly to the target class. You can't declare a validation that applies to all the fields of child objects that have the same name. It's extremely unlikely that two classes would have exactly the same field set and the same validation requirements, if that were the case then they are essentially the same class and may be should be inheriting from a common base class that has the validation metadata applied.
so should i create a separate metadata for accomodation and venue entity ,but how will the strongly type data will map the validation of both entity on a single view
You are providing the two classes to the view so the metadata will be applied. So yes, you need to set up metadata on each class.
|
1

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")
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.