TextBoxFor() not generating validation markup

北城以北 提交于 2019-12-02 03:55:02

You shouldn't really be binding your views directly to your data mapping entities. You should create view model classes to wrap the data you pass to and from your view and then populate your data objects from the controller.

You can then perform the required validation on your view model without affecting your generated mapping classes.

Model

public class AwardViewModel
{
    [Required, StringLength(30)]
    public string Title { get; set; }
    ....
}

View

@model AwardViewModel

@using (Html.BeginForm()) {
    @Html.EditorFor(m => m.Title)
    @Html.ValidationMessageFor(m => m.Title)
    ...
}

Controller

[HttpPost]
public ActionResult Create (AwardViewModel model)
{
    /* Create new AWARD in entity context, populate
       with relevant fields from model and commit. */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!