MVC editorfor不工作的集合 [英] MVC editorfor not working for collection

查看:111
本文介绍了MVC editorfor不工作的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类叫做事件和模型类叫做EventRange:

I have a model class called Events and a model class called EventRange:

public class Event
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public List<EventRange> RangesCollection { get; set; }
}

public class EventRange
{
    public int Id { get; set; }

    public string RangeName { get; set; }
    public string RangeDescription { get; set; }
    public int Capacitiy { get; set; }
}

正如你所看到的事件类包含了许多EventRanges列表作为用户应该能够增添了不少EventRanges的。

As you can see the Event class contains a List for as many EventRanges as the user should be able to add a lot of EventRanges.

我创建了一个称为事件视图,它动态地追加为evenrange的局部视图。用户可以例如preSS添加事件范围按钮5次,如果他想的5 EventRanges保存。

I have created a view called events, which dynamicly appends a partial view for the evenrange. The user can in example press the Add Event Range button 5 times if he want's 5 EventRanges saved.

事件视图:

@using (Ajax.BeginForm("CreateEvent", "Events", new AjaxOptions { HttpMethod = "POST" }, new { @class = "mainForm" }))
{
    @*Event data:*@
    @Html.LabelFor(m => m.Name)@Html.TextBoxFor(m => m.Name)
    @Html.LabelFor(m => m.Description)@Html.TextBoxFor(m => m.Description)

    @*EventRange data:*@

    <div id="EventRangediv">
    @Html.EditorFor(m => m.RangesCollection)
    </div>

}

对于eventrange局部视图下保存〜/视图/事件/ EditorTemplates / EventRange.cshtml

The partial view for the eventrange is saved under "~/views/events/EditorTemplates/EventRange.cshtml"

EventRange.cshtml:

EventRange.cshtml:

@model fanaticksMain.Models.EventRange

@Html.HiddenFor(m => m.Id)
@Html.DisplayFor(m => m.RangeName)
@Html.LabelFor(m => m.RangeName)@Html.TextBoxFor(m => m.RangeName)
@Html.LabelFor(m => m.RangeDescription)@Html.TextBoxFor(m => m.RangeDescription)

不过,加载事件视图不从partialview,对于名称和描述事件的工作标签和文本框加载任何元素。但没有什么是显示为eventrange局部视图。

However, loading the events view doesn't load any element from the partialview, the labels and textboxes for the name and description for the event work. But nothing is shown for the eventrange partial view.

任何人有任何想法,我做错了什么?此外,没有任何人有任何建议,如果这是绑定的集合张贴形式的正确方法?

Anyone have anyone idea what I'm doing wrong? Also, does anyone have any suggestion if this is the right way to bind a collection for posting a form?

推荐答案

EventRange.cshtml具有指定为EventRange模型。 m.RangesCollection是EventRange对象的集合。

EventRange.cshtml has the model specified as EventRange. m.RangesCollection is a collection of EventRange objects.

我不认为这是指接受EventRange集合的典范。

I don't see a model that is defined as accepting a collection of EventRange.

尝试的地方for循环中的code遍历每个EventRange并显示每个人的EventRange.cshtml编辑:

try place a for loop in your code to iterate each EventRange and display the EventRange.cshtml editor for each one:

而不是:

@Html.EditorFor(m => m.RangesCollection)

尝试:

@for (int i = 0; i < m.RangeCollection.Count; i++)
{
    @Html.EditorFor(m => m.RangeCollection[i])
}

这篇关于MVC editorfor不工作的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆