如何将项目添加到“List”在模型“与TextBoxFor,以方便不显眼的客户端验证MVC3?

发布于 2024-12-21 22:57:26 字数 661 浏览 1 评论 0原文

我在视图模型中使用一个列表,我希望使用不显眼的客户端验证模型来验证视图(使用 MVC3 上的 Razor 视图)。

我正在尝试从表单中收集新的人员信息并进行验证,然后将其添加到视图模型中的列表中。但是使用 TextBoxFor 我别无选择,只能使用集合中的特定项目,这不是它必须的工作方式。

任何帮助表示赞赏。

提前致谢。

public class Person
{
        [Required(ErrorMessage="First name is a Required Field")]        
        public string FirstName
        { get; set; }

        [Required(ErrorMessage = "Last name is a Required Field")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Primary E-Mail is a Required Field")]
        public string PrimaryEmail { get; set; }

        public string PrimaryPhoneNumber { get; set; }
}

I am using a List in my viewmodel that I wish to validate on the view(using Razor view on MVC3) using unobtrusive client validation with models.

I am trying to collect a new person info from form with validation and then adding it to the list in the view model. But using the TextBoxFor I have no choise but to use specific item in the collection which is not how it must work.

Any help is appreciated.

Thanks in advance.

public class Person
{
        [Required(ErrorMessage="First name is a Required Field")]        
        public string FirstName
        { get; set; }

        [Required(ErrorMessage = "Last name is a Required Field")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Primary E-Mail is a Required Field")]
        public string PrimaryEmail { get; set; }

        public string PrimaryPhoneNumber { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱我好吗 2024-12-28 22:57:26

我通过向 ViewModel 添加一个属性来实现此目的,该属性公开一个新的 person 实例。

public class PeopleModel
{
    public IEnumerable<Person> People { get; set; }
    public Person NewPerson { get; set; }
}

你甚至不需要给它赋值,属性的存在就足够了。

public ActionResult Index()
{
    var data = new PeopleModel {People = getPeople()};
    return View(data);
}

然后在您看来:

@using(Html.BeginForm("MakeNew", "People", FormMethod.Post))
{
    @Html.LabelFor(m => m.NewPerson.FirstName)
    @Html.TextBoxFor(m => m.NewPerson.FirstName)
}

最后在您接收新数据的操作中:

public ActionResult MakeNew(Person newPerson)
{
    return Content(newPerson.FirstName);
}

I've done this by adding a property to my ViewModel which exposes a new person instance.

public class PeopleModel
{
    public IEnumerable<Person> People { get; set; }
    public Person NewPerson { get; set; }
}

You don't even have to assign a value to it, the existence of the property is enough.

public ActionResult Index()
{
    var data = new PeopleModel {People = getPeople()};
    return View(data);
}

Then in your view:

@using(Html.BeginForm("MakeNew", "People", FormMethod.Post))
{
    @Html.LabelFor(m => m.NewPerson.FirstName)
    @Html.TextBoxFor(m => m.NewPerson.FirstName)
}

and finally in your action that receives the new data:

public ActionResult MakeNew(Person newPerson)
{
    return Content(newPerson.FirstName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文