如何在Controller中的另一个模型中绑定模型字段?

时间:2015-04-15 00:33:05

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

我有一个Model(Register.cs),其中包含另外两个Model(ProfileMeta,ProfileDetail)作为字段。如何在Controller类中绑定这两个模型字段?正如我所见,当我将2个字段对象命名为与模型文件名相同时。当我在ProfileController中引用它们时,它们被视为类型而不是对象。

Error   26  An object reference is required for the non-static field, method, or property 'ContosoUniversity.Models.ProfileMeta.password.get'   C:\Projects\DatingSite\datingSite\ContosoUniversity\Controllers\ProfileController.cs    63  40  DatingSiteInitial

但是,当我将它们重命名为不同的名称,并在ProfileController中像这样引用它时,我得到下面的编译错误:   “在当前背景下不存在”

型号/ Register.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContosoUniversity.Models
{
    public class Register
    {
        public ProfileMeta ProfileMeta { get; set; }
        public ProfileDetail ProfileDetail { get; set; }
    }
}

控制器/ ProfileController.cs:

// POST: Profiles/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 = "ProfileMeta,ProfileDetail")] Register register)
        {
            if (ModelState.IsValid)
            {
                //Add 1 ProfileMeta row and 1 linked ProfileDetail row
                ProfileMeta profileMeta = new ProfileMeta();
                //How to refer to bound: ProfileMeta, ProfileDetail fields inside Register.cs?
                profileMeta.Username = ProfileMeta.Username;
                profileMeta.password = ProfileMeta.password;
                profileMeta.ID = ProfileDetail.ID;
                db.ProfileDetails.Add(ProfileDetail);

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(profileDetail);
        }

查看/资料/ Create.cshtml:

@model ContosoUniversity.Models.Register
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


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

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

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

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

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

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

0 个答案:

没有答案