创建子表单以上载更新另一个实体

时间:2017-02-12 21:27:20

标签: c# asp.net asp.net-mvc

这让我很生气,因为它看起来像是一个基本的东西,但我不能为我的生活谷歌它... ...

我在MVC网站的投资组合部分有两个模型

public class PortfolioEntry
{
    public int ID { get; set; }
    [Display(Name="Project Name")]
    public string Name { get; set; }
    [Display(Name="Created for")]
    public string Client { get; set; }
    public string Description { get; set; }
    [Display(Name ="Started at")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Start { get; set; }
    [Display(Name ="Ended by")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime End { get; set; }
    [Display(Name ="Skills used")]
    public string Category { get; set; }
    [Display(Name ="GitHub Link")]
    public string GitHubLink { get; set; }
    [Display(Name ="Solution Screenshots")]
    public virtual ICollection<ScreenShot> ScreenShots { get; set; }
}

第二个:

    public enum ScreenShotType
    {
        [Display(Name = "Use case Diagram")]
        UseCase,
        [Display(Name = "Behaviour Diagram")]
        Behavior,
        [Display(Name ="Class Diagram")]
        ClassStructure,
        [Display(Name ="Main View")]
        MainPage,
        [Display(Name = "Additional View")]
        SomthingCool
    }
    public class ScreenShot
    {
        public int ID { get; set; }
        public string Description { get; set; }
        [Display(Name="Screenshot Type")]
        public ScreenShotType Type { get; set; }
        public string ImageURL { get; set; }
        public int Sorting { get; set; }
        public int PortfolioEntryID { get; set; }
    }
}

现在我要做的是创建一个子窗体,允许我动态上传ScreenShot并在数据库中为它创建一条新记录(点击浏览...,选择照片,填写信息然后点击&#34;上传&#34;)。 然后返回&#34; Main&#34;表格,所以我可以上传另一个或完全保存整个。

我应该怎么做?我尝试在控制器中创建一个新动作:

    public async Task<ActionResult> UploadPhoto([Bind(Include = "ID,Name,Client,Description,Start,End,GitHubLink")] PortfolioEntry portfolioEntry, HttpPostedFileBase uploadedFile,  string description, int sorting)
    {

        if (uploadedFile!=null && uploadedFile.ContentLength>0 && uploadedFile.ContentType.Contains("image"))
        {

            string extension = Path.GetExtension(uploadedFile.FileName.ToString().ToLower());
            string fileName = "PorfolioImage" + String.Format("{0:D5}", db.Photos.Count()) + extension;
            var path = Path.Combine(Server.MapPath("~/Pictures/ScreenShots"), fileName);
            uploadedFile.SaveAs(path);
            var screenshotToAdd = new ScreenShot
            {
                Description = description,
                Sorting = sorting,
                PortfolioEntryID = portfolioEntry.ID,
                ImageURL = fileName                    
            };
            await db.SaveChangesAsync();
        }
        return(View(portfolioEntry));
    }

但:  1.它没有看到上传的文件  2.即使它确实看到了文件,它似乎也没有从Bind注册实体

我已经看过一个解决方案:mkozicki但在重新编写方法之前,我想知道这是否是我应该采取的路径。

亚当

编辑:根据后续添加视图:

@model KoscielniakInfo.Models.PortfolioEntry

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>PortfolioEntry</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Client, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Client, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Client, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Start, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Start, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Start, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.End, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.End, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.End, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.GitHubLink, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.GitHubLink, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.GitHubLink, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">

            <div class="col-md-offset-1 col-md-10">
                <table>
                    <tr>
                        @{
                            int cnt = 0;
                            foreach (var category in ViewBag.Categories)
                            {

                                if (cnt++ % 3 == 0)
                                {
                                    @Html.Raw("</tr><tr>")
                                }
                                @Html.Raw("<td>")
                                <input type="checkbox"
                                       name="selectedCategories"
                                       value="@category.Name"
                                       @(Html.Raw(category.Selected ? "checked=\"checked\"" : "")) />


                                    @category.Name
                                    @Html.Raw("</td>")

                            }

                            @Html.Raw("</tr>")
                        }
                </table>
            </div>
        </div>

        <div class="form-group">
            <div id="newCats" class="col-md-10 col-md-offset-1">
                <div id="newCat">
                    <h5>
                        New Category
                    </h5>
                    <input type="text" name="newCategories" /><br />
                </div>
            </div>
            <div class="col-md-10 col-md-offset-1">
                <input id="clone" type="button" value="More Categories" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>

 @*=================================This is the form Group for adding the photo ============================================*@
    <div class="form-group">
        <div class="row">
            <div class="col-md-offset-2 col-md-3">
                Description:
            </div>
            <div class="col-md-7">
                <input type="text" name="description" id="description" />
            </div>
        </div>
        <div class="row">
            <div class="col-md-offset-2 col-md-3">
                screenType:
            </div>
            <div class="col-md-7">
                <input type="text" name="screenType" />
            </div>
        </div>
        <div class="row">
            <div class="col-md-offset-2 col-md-3">
                Sorting:
            </div>
            <div class="col-md-7">
                <input type="number" name="sorting" id="sorting" />
            </div>
        </div>
        <div class="row">
            <div class="col-md-offset-2 col-md-3">
                Image:
            </div>
            <div class="col-md-7">
                <input type="file" name="uploadedFile" />
            </div>
            <input type="button" value="Create" onclick="location.href='@Url.Action("UploadPhoto", "PorfolioEntries")'" />
        </div>
    </div>
//=============================================Ends Here =========================================================================
                            }

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>

        $('#clone').click(function () {
            $('#newCat').last().clone().appendTo('#newCats')
        })

    </script>
}

0 个答案:

没有答案