Прежде чем приступить к реализации этой задачи, я настоятельно рекомендую вам прочитать Редактирование списка переменной длины в стиле ASP.NET MVC 2 от Стивена Сандерсона.
Готовый?
Хорошо, теперь мы можем перейти к реализации.
Первым делом нужно определить нашу модель представления для задачи. Он у вас уже есть, просто определите для него соответствующие правила проверки:
public class Person
{
[Required]
public string FullName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Address{ get; set; }
}
и я полагаю, что моей моделью для этой страницы должен быть список
Да, конечно.
Итак, давайте продолжим и создадим наш PersonsController
:
public class PersonsController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new Person()
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<Person> persons)
{
if (!ModelState.IsValid)
{
return View(persons);
}
// To do: do whatever you want with the data
// In this example I am simply dumping it to the output
// but normally here you would update your database or whatever
// and redirect to the next step of the wizard
return Content(string.Join(Environment.NewLine, persons.Select(p => string.Format("name: {0} address: {1}", p.FullName, p.Address))));
}
public ActionResult BlankEditorRow()
{
return PartialView("_PersonEditorRow", new Person());
}
}
А теперь давайте определим представление (~/Views/Persons/Index.cshtml
):
@model IEnumerable<Person>
@using (Html.BeginForm())
{
<div id="editorRows">
@foreach (var item in Model)
{
Html.RenderPartial("_PersonEditorRow", item);
}
</div>
@Html.ActionLink(
"Add another person",
"BlankEditorRow",
null,
new { id = "addItem" }
)
<p>
<button type="submit">Next step</button>
</p>
}
<script type="text/javascript">
$('#addItem').click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $('#editorRows').append(html); }
});
return false;
});
$(document).delegate('a.deleteRow', 'click', function () {
$(this).parents('div.editorRow:first').remove();
return false;
});
</script>
и соответствующий частичный вид (~/Views/Persons/_PersonEditorRow.cshtml
):
@model Person
<div class="editorRow">
@using(Html.BeginCollectionItem("persons"))
{
<div>
@Html.LabelFor(x => x.FullName)
@Html.EditorFor(x => x.FullName)
@Html.ValidationMessageFor(x => x.FullName)
</div>
<div>
@Html.LabelFor(x => x.LastName)
@Html.EditorFor(x => x.LastName)
@Html.ValidationMessageFor(x => x.LastName)
</div>
<div>
@Html.LabelFor(x => x.Address)
@Html.EditorFor(x => x.Address)
@Html.ValidationMessageFor(x => x.Address)
</div>
<a href="#" class="deleteRow">delete</a>
}
</div>
Примечание. Используемый здесь помощник Html.BeginCollectionItem
взят из сообщения в блоге Стивена Сандерсона, на которое я ранее ссылался в своем ответе и который вы уже прочитали и с которым вы знакомы. Вот исходный код для полноты:
public static class HtmlPrefixScopeExtensions
{
private const string idsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_";
public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName)
{
var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName);
string itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().ToString();
// autocomplete="off" is needed to work around a very annoying Chrome behaviour whereby it reuses old values after the user clicks "Back", which causes the xyz.index and xyz[...] values to get out of sync.
html.ViewContext.Writer.WriteLine(string.Format("<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />", collectionName, html.Encode(itemIndex)));
return BeginHtmlFieldPrefixScope(html, string.Format("{0}[{1}]", collectionName, itemIndex));
}
public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
}
private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName)
{
// We need to use the same sequence of IDs following a server-side validation failure,
// otherwise the framework won't render the validation error messages next to each item.
string key = idsToReuseKey + collectionName;
var queue = (Queue<string>)httpContext.Items[key];
if (queue == null)
{
httpContext.Items[key] = queue = new Queue<string>();
var previouslyUsedIds = httpContext.Request[collectionName + ".index"];
if (!string.IsNullOrEmpty(previouslyUsedIds))
foreach (string previouslyUsedId in previouslyUsedIds.Split(','))
queue.Enqueue(previouslyUsedId);
}
return queue;
}
private class HtmlFieldPrefixScope : IDisposable
{
private readonly TemplateInfo templateInfo;
private readonly string previousHtmlFieldPrefix;
public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
this.templateInfo = templateInfo;
previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
}
public void Dispose()
{
templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
}
}
}
ОБНОВИТЬ:
Плохо, я только что заметил, что ваш вопрос помечен тегом asp.net-mvc-2
. Поэтому я думаю, что мои взгляды на Razor не применимы к вашему случаю. Тем не менее, все остальное должно работать так же. Все, что вам нужно сделать, это обновить представления, чтобы они использовали механизм представления WebForms:
Вот ~/Views/Persons/Index.aspx
:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Person>>" %>
<% using (Html.BeginForm()) { %>
<div id="editorRows">
<% foreach (var item in Model) { %>
<% Html.RenderPartial("_PersonEditorRow", item); %>
<% } %>
</div>
<%= Html.ActionLink(
"Add another person",
"BlankEditorRow",
null,
new { id = "addItem" }
) %>
<p>
<button type="submit">Next step</button>
</p>
<% } %>
<script type="text/javascript">
$('#addItem').click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $('#editorRows').append(html); }
});
return false;
});
$(document).delegate('a.deleteRow', 'click', function () {
$(this).parents('div.editorRow:first').remove();
return false;
});
</script>
и, наконец, часть (~/Views/Persons/_PersonEditorRow.ascx
):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<div class="editorRow">
<% using(Html.BeginCollectionItem("persons")) { %>
<div>
<%= Html.LabelFor(x => x.FullName) %>
<%= Html.EditorFor(x => x.FullName) %>
<%= Html.ValidationMessageFor(x => x.FullName) %>
</div>
<div>
<%= Html.LabelFor(x => x.LastName) %>
<%= Html.EditorFor(x => x.LastName) %>
<%= Html.ValidationMessageFor(x => x.LastName) %>
</div>
<div>
<%= Html.LabelFor(x => x.Address) %>
<%= Html.EditorFor(x => x.Address) %>
<%= Html.ValidationMessageFor(x => x.Address) %>
</div>
<a href="#" class="deleteRow">delete</a>
<% } %>
</div>
person
Darin Dimitrov
schedule
02.01.2013