For the sake of validation, I decided to try using the @Html.TextBoxFor so that validation is quick and easy. However, there is not an option to set the value, and I assume it’s done automatically, but it isn’t being automatically set.
Controller:
MyLibrary.MyProspect prospect = MyLib.GetProspect(ID);
return View(prospect);
View:
@model MyLibrary.MyProspect
@using (Html.BeginForm("Update", "Prospects"))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit</legend>
@Html.LabelFor(model => model.Name)
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
My library defination of Prospect:
public class MyProspect
{
public String Name { get; set; }
}
My guess is that at no point do I set the model to actually be my prospect. But no examples I have come across do this, and I assume I pass it from view as I have?
SOLVED
This is working code, Id just mixed and matched my controller action to the wrong view.
For client side validation to work there are a couple of things you need to do:
ClientValidationEnabled&UnobtrusiveJavaScriptEnabledoptions set totrue. This can be done directly in the<appSettings>.jquery.validate.min.jsandjquery.validate.unobtrusive.min.jsAlso if you have no special view requirements you could reduce your code to: