与父视图更新儿童asp.net mvc的父子视图 [英] asp.net mvc parent child view with parent view updating children

查看:103
本文介绍了与父视图更新儿童asp.net mvc的父子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是局部视图创建父子视图。我理想的情况是一样的提交父视图按钮保存子值。

I am using a partial view to create a parent child view. What I would ideally like is the submit button on the parent view to save the child values.

我有以下的模型。

public class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public int Par { get; set; }
    public string Tee { get; set; }
    public decimal Rating { get; set; }
    public virtual IEnumerable<CourseHole> Holes { get; set; }

    public static Course Create()
    {
        var course = new Course();
        course.Par = 72;
        var holes = new List<CourseHole>();
        for (int i = 0; i < 18; i++)
        {
            holes.Add(new CourseHole() { Course = course, Number = i + 1, Par = 4 });
        }
        course.Holes = holes;
        return course;
    }
}

public class CourseHole
{
    public int CourseHoleId { get; set; }
    public int Number { get; set; }
    public int Par { get; set; }
    public int Length { get; set; }
    public int Ranking { get; set; }
    public Course Course { get; set; }
}

和下面的父视图。

@model Golf_Statz.Models.Course

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


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

    <div class="form-horizontal">
        <h4>Course</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-4">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>

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

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

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

        @foreach (Golf_Statz.Models.CourseHole hole in Model.Holes)
        {
            @Html.Partial("CreateHole", hole)
        }

        <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>
}



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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

和局部视图以下。

@model Golf_Statz.Models.CourseHole

@{
    ViewBag.Title = "CreateHole";
}

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

    <div class="form-horizontal" id="CreateHole-" + model.CourseHoleId>
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-2 col-md-offset-2">
                <p>@Model.Number</p>
            </div>
            <div class="col-md-2">
                @Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-2">
                @Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-2">
                @Html.EditorFor(model => model.Ranking, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Ranking, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我的控制方法。

// GET: Course/Create
        public ActionResult Create()
        {           
            return View(Course.Create());
        }

        // POST: Course/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "CourseId,Name,Par,Tee,Rating")] Course course)
        {
            if (ModelState.IsValid)
            {
                db.Courses.Add(course);
                foreach (var hole in course.Holes)
                {
                    db.CourseHoles.Add(hole);
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(course);
        }

无论我似乎做course.Holes在HttpPost Create方法总是空。
我想,我想类似的东西,以<一个href=\"http://stackoverflow.com/questions/1103573/updating-parent-child-records-with-model-binders-in-asp-net-mvc\">this但它的编辑,我希望它创建。
任何帮助将是pciated大大AP $ P $,因为这是我的第一个MVC项目。

No matter what I seem to do course.Holes in the HttpPost Create method is always null. I think I want something similar to this but it's for editing and I want it for creating. Any help would be greatly appreciated as this is my first mvc project.

推荐答案

的foreach 循环产生重复的 ID 属性(无效的HTML)和名称属性,其中有你的模型没有任何关系。更改局部到 EditorTemplate

Your foreach loop is generating duplicate id attributes (invalid html) and name attributes which have no relationship to your model. Change the partial to an EditorTemplate

/Views/Shared/EditorTemplates/CourseHole.cshtml

/Views/Shared/EditorTemplates/CourseHole.cshtml

和删除 BeginForm AntiForgeryToken 和脚本

@model Golf_Statz.Models.CourseHole
<div class="form-horizontal" id="CreateHole-" + model.CourseHoleId>
    <div class="form-group">
        <div class="col-md-2 col-md-offset-2">
            <p>@Model.Number</p>
        </div>
        <div class="col-md-2">
            @Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-2">
            @Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-2">
            @Html.EditorFor(model => model.Ranking, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Ranking, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

,然后在主视图

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

EditorFor()方法将正确生成的HTML绑定到一个集合,例如:

The EditorFor() method will correctly generate the html for binding to a collection, for example

<input name="Holes[0].Par" ...>
<input name="Holes[1].Par" ...>

您还需要删除 [绑定] 属性,因为你是不包括物业,你好像要绑定到所有属性呢。

You also need to remove the [Bind] attribute since you are excluding property Holes, and you seem to want to bind to all properties anyway.

旁注:你不产生孔 CourseHoleId 编号属性,所以这不会回传的输入

Side note: You do not generate an input for the hole CourseHoleId or Number properties so these wont post back.

这篇关于与父视图更新儿童asp.net mvc的父子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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