0

I have a ViewModel with [Required] and [MaxLength(4)] attribute

public class Student
{
    [MaxLength(4)]
    [Required]
    public string Name { get; set; }
}

Inside my view I have

@model List<WebApplication2.Models.Student>
<div class="row">
    <div class="col-md-4">
        @{ 
            foreach(var item in @Model)
            {
                @Html.TextBoxFor(model=>item.Name)
                @Html.ValidationMessageFor(model => item.Name)
            }
        }
      </div>
    <div class="col-md-4">
        @{
            foreach (var item in @Model)
            {
                <input type="text" value="@item" />
            }
        }
    </div>

When I use @Html helpers to render text boxes it applied data-validation rules to textbox. I couldn't figure out how I can apply that to normal Html <input type = 'text' /> Is there any way I can do that dynamically without using @Html helpers?

  • Why would you want to do it manually? And what do you mean _dynamically_? –  Apr 26 '17 at 22:56
  • And as a side note, your `foreach` loop will not bind to your model or give you validation- refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). Also your `` makes no sense - `item` is a complex object and you cannot bind an input to a complex object –  Apr 26 '17 at 22:57
  • a for loop would do with @Html helpers. dynamically means, the model properties has custom attributes with I can obtain with model.GetType().GetProperty("Name") but not sure how to do something to make fetch and apply validation rules when this renders – InTheWorldOfCodingApplications Apr 26 '17 at 23:01

0 Answers0