简体   繁体   English

剃刀:处理视图中的对象列表以及模型验证

[英]Razor: Handling a list of objects within a view, and model validation

I have two classes like this (to make it simple) 我有两个这样的类(为简单起见)

public class Page
{
    int pageNumber;
    string content;
}

public class Book
{
    public string name;
    public List<Page> pages;
}

Now, in a razor view, I have a form allowing to add a book, with a list of pages in the form, to add pages. 现在,在剃刀视图中,我有一个表单,可以添加一本书,并在表单中添加页面列表。

Currently, the pages are added dynamically to the form, and I get the values in my controller using Request.Form for building my list of Pages in the controller. 当前,页面是动态添加到表单中的,我可以使用Request.Form在控制器中获取值,以在控制器中构建页面列表。 The problem is, how can I validate the model with the pages (something like a page must have a content not empty for example) before entering in the action in the controller. 问题是,在控制器中输入操作之前,如何在页面上验证模型(例如,页面必须具有不为空的内容)。

The fields are added with JQuery in the form. 这些字段以JQuery的形式添加。 I think it not possible to bind a list of Pages directly to the model in the view, especially when the fields are generated with javascript. 我认为将页面列表直接绑定到视图中的模型是不可能的,尤其是当使用javascript生成字段时。 But maybe I am missing something. 但是也许我错过了一些东西。

Thanks 谢谢

you can validate the pages in JQuery before the data is send to the server. 您可以在将数据发送到服务器之前验证JQuery中的页面。

or you can user a filter. 或者您可以使用过滤器。 a filter is a method that is executed before an action is executed. 过滤器是在执行操作之前执行的方法。 (you may have seen or used the [Authorize] filter) (您可能已经看过或使用过[Authorize]过滤器)

you can create a filter that validates the data and if the validation fails the request will be redirected to an error page. 您可以创建一个过滤器来验证数据,如果验证失败,请求将被重定向到错误页面。

here is a tutorial 这是一个教程

here is another tutorial 这是另一个教程

You can use model binding in ASP.NET, even if you're adding jquery form elements, see this to create collections that are binding to model: 您可以在ASP.NET中使用模型绑定,即使您要添加jquery表单元素,也请参见以下内容以创建绑定到模型的集合:

ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries 用于模型绑定到数组,列表,集合,字典的ASP.NET线格式

I think your form can be done as that in order to run model binding correctly: 我认为您可以按照这种方式完成表格,以便正确运行模型绑定:

<input type="text" name="name"/>
<input type="text" name="pages[0].pageNumber"/>
<input type="text" name="pages[0].content" />
<input type="text" name="pages[1].pageNumber"/>
<input type="text" name="pages[1].content"/>
<input type="text" name="pages[3].pageNumber"/>
<input type="text" name="pages[3].content"/>

With that use, you can receive the Book object from the view to your controller: 通过这种用法,您可以从视图向控制器接收Book对象:

public ActionResult Create(Book myBook)
    {}

Then, for validation, I suggest you tu use Data Annotation to your model, use [Required] tag before the content property of page object, or [MinLenght], see this Microsoft documentation about Data Annotations. 然后,为了进行验证,建议您对模型使用数据注释,在页面对象的content属性之前使用[Required]标签,或使用[MinLenght],请参阅有关数据注释的Microsoft文档

[Required]
public string Content { get; set; }

Then in your view, use jquery.validate (don't forget to enable it in the view) and in the controller, when you receive your model, you can check the modelstate with 然后在您的视图中使用jquery.validate(不要忘记在视图中启用它),并在控制器中,当您收到模型时,可以使用以下命令检查modelstate

public ActionResult Create(YourObject object)
{
    if (ModelState.IsValid)
    {
            // code when model is valid
    }
}

With MVC you can use DataAnnotations on your model properties. 使用MVC,您可以在模型属性上使用DataAnnotations。

Using using System.ComponentModel.DataAnnotations; 使用using System.ComponentModel.DataAnnotations;

public class Book{

    [Required]
    public string Name{ get; set; }

    public List<Page> Pages { get; set; }
}

public class Page{

    [Required]
    public int PageNumber{ get; set; }

    [Required]
    public string Content { get; set; }
}

For a deeper understanding read this: http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model 为了更深入地了解,请阅读以下内容: http : //www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM