1

我正在尝试使用 .net 4.5、Entity 5、MVC 4 创建一个简单的多模型表单

根据我看到的示例,我希望 EditorTemplates\Tasks.cshtml 被渲染两次,一次为项目中的每个任务,但它似乎只被调用一次并传递了我似乎无法使用的任务集合。

有任何想法吗?我的课程/观点如下所列。

任务:

public partial class Task
{
    public Task()
    {
        this.TaskPeople = new HashSet<TaskPerson>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public int ProjectId { get; set; }

    public virtual Project Project { get; set; }
    public virtual ICollection<TaskPerson> TaskPeople { get; set; }
}    

项目:

public partial class Project
{
    public Project()
    {
        this.Tasks = new HashSet<Task>();
    }

    public int Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Task> Tasks { get; set; }
}

HomeController.New():

public ActionResult New()
{
    Project project = new Project();
    project.Tasks.Add(new Task() { });
    project.Tasks.Add(new Task() { });
    return View(project);
}

主页/New.cshtml:

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{    
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()    
    @Html.LabelFor(m => m.Title)    
    @Html.EditorFor(m => m.Tasks, "Tasks")
    <input type="submit" value="Save" />
}

主页/EditorTemplates/Tasks.cshtml

@model IEnumerable<MvcApplication5.Models.Task>

@Html.TextBoxFor(m => m.Title) // Error Here

编辑:

如果我从 Tasks.cshtml 中取出 IEnumerable 并将其替换为

@model MvcApplication5.Models.Task

我得到以下异常:

The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[MvcApplication5.Models.Task]', but this dictionary requires a model item of type 'MvcApplication5.Models.Task'.
4

3 回答 3

2

但是,在Home/EditorTemplates/Tasks.cshtml模型IEnumerable中,要呈现文本框,您不会遍历列表。

不确定当您尝试访问该Title属性时如何没有收到运行时错误,因为该属性在IEnumerable<MvcApplication5.Models.Task>

于 2013-10-07T22:34:00.550 回答
1

您对模板的理解有点偏离,所以让我看看是否可以提供帮助。

和助手Html.Display*Html.Editor*能够识别您何时向他们传递集合。您不必遍历该集合,这些方法将为该集合的每个元素重复调用集合的基础类型的模板。

用简单的英语,如果您将 a 传递List<Car>给其中一个方法,如果Car.cshtml模板存在,它将Car为该列表中的每个重复调用该模板。在您的情况下,这意味着将您的代码更改为以下内容:

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{    
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()    
    @Html.LabelFor(m => m.Title)    
    @Html.EditorFor(m => m.Tasks)
    <input type="submit" value="Save" />
}

然后模板Task.cshtml

@model MvcApplication5.Models.Task

@Html.TextBoxFor(m => m.Title)
于 2013-10-07T22:44:00.223 回答
0

这个型号

public class TSubject
{
    public string Title { get; set; }
    public bool IsSelected { get; set; }
}
public class TMonth
{
    public string Title { get; set; }
    public bool IsSelected { get; set; }
}


public class TModel
{
    public List<TSubject> Subjects { get; set; }
    public List<TMonth> Months { get; set; }
}

控制器

[HttpGet]
    public ActionResult Test1()
    {
        TModel model = new TModel
        {
            Months = new List<TMonth>
            {
                new TMonth
                {
                    IsSelected = false, Title = "January"
                },
                new TMonth
                {
                    IsSelected = true, Title = "Feburary"
                },
                new TMonth
                {
                    IsSelected = false, Title = "March"
                },
                new TMonth
                {
                    IsSelected = true, Title = "April"
                },
                new TMonth
                {
                    IsSelected = true, Title = "May"
                },
                new TMonth
                {
                    IsSelected = false, Title = "June"
                },
                new TMonth
                {
                    IsSelected = false, Title = "July"
                },
                new TMonth
                {
                    IsSelected = true, Title = "August"
                },
            },
            Subjects = new List<TSubject>
            {
                new TSubject
                {
                    Title = "Biology", IsSelected = false, 
                },
                new TSubject
                {
                    Title = "Chemistry", IsSelected = false,
                },
                new TSubject
                {
                    Title = "Physics", IsSelected = true,
                },
                new TSubject
                {
                    Title = "English", IsSelected = false,
                },
                new TSubject
                {
                    Title = "Urdu", IsSelected = false,
                },
                new TSubject
                {
                    Title = "Islamic Studies", IsSelected = true,
                },
            }
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult Test1(TModel mod)
    {
        if(ModelState.IsValid)
        {
            ViewBag.Title = "All OK";
        }
        return View(mod);
    }

HTML 视图

@model ZiaratToursMVC.ZiaratModels.TModel

<h1>Subject list</h1>
<table class="table table-bordered">
    <tr>
        <th>Subject</th>
        <th>Is selected</th>
    </tr>
    <tbody>
        @Html.EditorFor(a => a.Subjects)
    </tbody>
</table>
<p></p>
<h1>Month list</h1>
<table class="table table-bordered">
    <tr>
        <th>Month</th>
        <th>Is selected</th>
    </tr>
    <tbody>
        @Html.EditorFor(a => a.Months)
    </tbody>
</table>

TMonth.cs 编辑器模板

@model ZiaratToursMVC.ZiaratModels.TMonth

<tr>
<td>
    @Html.HiddenFor(a => a.Title)
    @Model.Title
</td>
<td>
    @Html.CheckBoxFor(a => a.IsSelected)
</td>

TSubject.cs 编辑器模板

@model ZiaratToursMVC.ZiaratModels.TSubject

<tr>
<td>
    @Html.HiddenFor(a => a.Title)
    @Model.Title
</td>
<td>
    @Html.CheckBoxFor(a => a.IsSelected)
</td>
于 2021-11-06T15:48:27.627 回答