You should be using @Html.TextboxFor
instead of @Html.Textbox
<p>
@Html.LabelFor(model => Model.Dinner.EventDate)
<br />
@Html.TextBoxFor(model => Model.Dinner.EventDate)
@Html.ValidationMessageFor(model => Model.Dinner.EventDate)
</p>
The basic @Html.Textbox
will render a textbox with the name attribute specified in the string that you are passing. MVC will look into the ViewBag to see if there are any items with that key to populate the textbox (which is why your Title attribute is using the page title from the top section of your view), but does not tie the view inputs to the actual model or any pre-populated data that you sent with the model. By using the TextBoxFor (or labelFor, etc), this ties that input to the actual model property. This is also how DataAnnotations are applied to the form. What I mean by that last statement is this. Let's assume this is a piece of your model
public class DinnerViewModel{
[DisplayName("Dinner Location")]
[Required(ErrorMessage="You must specify a location")]
public string Location {get;set;}
}
In your view, you would render the pieces that you need by using the @Html.*For method (like you do your labels)
<p>
@Html.LabelFor(model => Model.Location )
<br />
@Html.TextBoxFor(model => Model.Location)
@Html.ValidationMessageFor(model => Model.Location )
</p>
Should render some HTML (not including the error message) like this
<p>
<label for="Location">Dinner Location</label>
<br/>
<input type="text" name="Location" id="Location"/>
*the validation related stuff*
</p>
Addendum
In order for your validation to work with the method that you are using, you have to change your yield return statements slightly. If you look closely at the id's for the attributes of the actual dinner object in the HTML source, you will see that they are rendering as 'Dinner.Title
' or 'Dinner.Description
'. This is because that is how they are stored in your model (remember using model => Model.Dinner.EventDate
?) This will render an element with the id 'Dinner.EventDate
'.
Given that, you need to update the strings being returned from the RuleViolation portion of your model:
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Dinner.Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required", "Dinner.Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "Dinner.HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Dinner.Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
{
yield return new RuleViolation("ContactPhone required", "Dinner.ContactPhone");
}
else
{
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "Dinner.ContactPhone");
}
yield break;
}
Now your RuleViolations will match the actual input IDs and everything will be shiny and awesome again. This seems like a bit of work, but since you are working through a tutorial, I don't want to push too much your way. However, as your explore the .NET MVC implentations, you will find other ways to accomplish the same tasks in a less verbose manner. Keep at it!