本文介绍了基于同一个模型的多个部分景观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

主要观点:

@model AuthorViewModel
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id="someId"        })) {

 @Html.LabelFor(model => model.Name);
 @Html.EditorFor(model => model.Name);
 @Html.ValidationMessageFor(model => model.Name);

 <label> Book </label>
 @{Html.RenderPartial("_BookView", new BookViewModel());}
 <label>One more book...</label>
 @{Html.RenderPartial("_BookView", new BookViewModel());}
}

局部视图:

@model BookViewModel
@Html.LabelFor(model => model.Title);
@Html.EditorFor(model => model.Title);
@Html.ValidationMessageFor(model => model.Title);

AuthorViewModel:

AuthorViewModel:

public class AuthorViewModel
{
    [Required]
    [DataType(DataType.Text)]
    public String Name { get; set; }
}

BookViewModel:

BookViewModel:

public class BookViewModel
{
    [Required]
    [DataType(DataType.Text)]
    public String Title { get; set; }
}

因此​​,当它呈现 - 它看起来的权利,但验证是所有书籍一样。一个我需要有大量的书籍(比如动态地添加的话)的作者和每个人必须学会独立和可验证。

So when it renders - it looks right, but validation is the same for all books. An I need to have a lot of books(say to add them dynamically) for author and each one have to be independent and "validatable".

我怎么能进行这样的行为?

How can I perform such behaviour?

推荐答案

我会 BookViewModel 的集合在你的 AuthorViewModel 。这样的名称和ID将是独一无二的。

I would have a collection of BookViewModel in your AuthorViewModel. That way the names and ids will be unique.

这篇关于基于同一个模型的多个部分景观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:30