I have razor file where I define html form with text box for string:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
what does your viewmodel look like?
You can add a DataAnnotation attribute to your Name property in your viewmodel:
public class MyViewModel
{
[Required(ErrorMessage="This field can not be empty.")]
public string Name { get; set; }
}
Then, in your controller you can check whether or not the model being posted is valid.
public ActionResult MyAction(ViewModel model)
{
if (ModelState.IsValid)
{
//ok
}
else
{
//not ok
}
}