使用子集合创建父对象 - “Framework 4.1 MVC 3.0”

时间:2012-04-10 10:12:11

标签: asp.net-mvc-3 razor ado.net entity-framework-4.1 edmx-designer

我想知道如何在一个步骤中使用其子集合创建父对象 使用ADO.net实体数据模型。

示例: 这是父对象“视频”及其子视频数据图

parent object "Video" and its Child VideoData

我需要使用视频数据创建视频对象

这是脚手架选项

创建的剃刀视图
@model Test.Models.Video

@{
    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>Video</legend>

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

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

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

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

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

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

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

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

您可以注意到,剃刀视图不包含与相关视频数据实体关联的任何字段。

我的问题是:如何将videoData添加到视频创建过程中。

如果有帮助,请HttpPost

中的VideoController创建
[HttpPost]
    public ActionResult Create(Video video)
    {
        if (ModelState.IsValid)
        {
            db.Videos.AddObject(video);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(video);
    }

我需要发布的视频对象包含其关联的VideoData collection

请咨询

0 个答案:

没有答案