创建MVC 3应用程序的模型对象属性的看法?

创建MVC 3应用程序的模型对象属性的看法?

问题描述:

我有一个数据库,顾问,由EF访问的Asp.Net MVC 3应用程序。现在,在数据库顾问表有一个一对多的关系,其他几个表的简历类型信息(工作经验等)。因此,用户应该能够填写自己的姓名等一次,但应该能够增加一些工作经验,等等。

I have an Asp.Net MVC 3 application with a database "Consultants", accessed by EF. Now, the Consultant table in the db has a one-to-many relationship to several other tables for CV type information (work experience, etc). So a user should be able to fill in their name etc once, but should be able to add a number of "work experiences", and so on.

但这些外键的表在模型复杂的对象,并在创建创建视图的时候我只得到简单的属性编辑器领域。我该如何去设计一个或多个视图,使复杂的物体可以填写呢?我想象我心中的图,其中简单属性是简单字段,然后某种形式的控制,在那里你可以点击增加工作经验,以及多达需要将增加。不过,我会怎么做,并仍在利用模型绑定?其实,我不知道如何去了解它。 (顺便说一句,程序和语言代表之类的东西在通用软件的经验,和自然语言的能力,而不是编程语言,如果你想知道有关的关系存在)。

But these foreign key tables are complex objects in the model, and when creating the Create View I only get the simple properties as editor fields. How do I go about designing the View or Views so that the complex objects can be filled in as well? I picture a View in my mind where the simple properties are simple fields, and then some sort of control where you can click "add work experience", and as many as needed would be added. But how would I do that and still utilize the model binding? In fact, I don't know how to go about it at all. (BTW, Program and Language stand for things like software experience in general, and natural language competence, not programming languages, in case you're wondering about the relationships there).

任何想法大大AP preciated!

Any ideas greatly appreciated!

下面是由加载视图命令默认创建的创建视图:

Here's the Create View created by the add View command by default:

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Consultant</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

和这里的EF数据库图:

And here's the EF database diagram:

更新:

据建议,我想史蒂芬·桑德森的博客解决方案,但我不能让它正常工作。我说这在主视图:

According to suggestion, I tried Steven Sanderson's blog solution, but I can't get it to work properly. I added this in the main view:

    <div id="editorRows">
        @foreach (var item in Model.Programs)
        {
            Html.RenderPartial("ProgramEditorRow", item);
        }
    </div>
<input type="button" value="Add program" id="addItem" />

我还添加了JavaScript的:

I also added the javascript:

<script type="text/javascript">
    var url = '@Url.Action("BlankEditorRow", "Consultant")';
    $(document).ready(function () {
        $("#addItem").click(function () {
            $.ajax({

                url: url,
                cache: false,
                success: function (html) {
                    alert(html);
                 $("#editorRows").append(html); }
            });
            return false;
        });
    });
</script>

一个局部视图:

@model Consultants.Models.Program
@using Consultants.Helpers

<div class="editorRow">
@*@using (Html.BeginCollectionItem("programs"))
{*@
    @Html.TextBoxFor(model => model.Name)    
@*}*@
</div>

和控制器的操作方法:

    public ActionResult Create()
    {
        Consultant consultant = new Consultant();

        return View(consultant);
    }


    public ActionResult BlankEditorRow()
    {
        return PartialView("ProgramEditorRow", new Program());
    }

请注意,我注释掉Html.BeginCollectionItem部分的局部视图,因为我不能得到那个工作。它只是给我的隐藏字段史蒂芬·桑德森谈论,但不是实际的文本框。所以,我想评论的那部分,只是有一个文本框。嗯,这让我的文本框,但我不能让在岗方法的信息。我用的是咨询对象作为返回参数,但程序属性中没有计划。也许这是与事实,我不能让BeginCollectionItem帮手工作要做,但在任何情况下,我不知道如何做到这一点,或者怎么去在视图中添加所谓的计划。用一个简单的对象,我会通过类似_repository.AddToConsultants(顾问)在岗方法中添加新的对象,当我保存到EF它得到其ID。但如何做同样的事情与程序对象,并通过它EF保存到数据库?

Note that I commented out the Html.BeginCollectionItem part in the partial view, because I can't get that to work. It only gives me the hidden field Steven Sanderson talks about, but not the actual textbox. So I tried commenting that part out and just had a textbox. Well, that gets me the textbox, but I can't get to that info in the post method. I use the Consultant object as return parameter, but the Programs property contains no Program. Perhaps this has to do with the fact that I cannot get the BeginCollectionItem helper to work, but in any case I don't understand how to do that, or how to get to the Program supposedly added in the view. With a simple object I would add the new object in the post method by something like _repository.AddToConsultants(consultant), and when I save to EF it gets its id. But how do I do the same thing with the program object and save it to the database via EF?

菲尔哈克写了一个巨大的博客文章,解释如何绑定模型的列表。这是具体到MVC2,但我不知道,如果第3版已在此提升。

Phil Haack has written a great blog post which explains how to model bind to a list. It's specific to MVC2, but I'm not sure if version 3 has improved on this.

模型绑定到一个List

在用户被允许添加项目任意数量的情况下,您可能会想创建使用JavaScript新的输入字段。史蒂芬·桑德森在这里展示了如何做到这一点:

In a scenario where the user is allowed to add an arbitrary number of items, you'll probably want to create new input fields using JavaScript. Steven Sanderson shows here how to achieve that:

编辑一个可变长度的名单,ASP.NET MVC 2式

这些资源应该让你一路都有。

Those resources should get you all the way there.