当我点击“添加部门”时,我会/Department/Create?contactId=1
:
这是“创建”视图。我把它填好,然后点击“创建”:
这是我单击“创建”后逐步执行的代码。请注意,该对象为空:
但是,“contactId”不为空。正如查询字符串所预期的那样,该值为“1”。这是我的创建视图:
@model CallCOP.Models.Department
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.dept_id)
@Html.HiddenFor(model => model.contact_id)
<fieldset>
<legend>Department</legend>
<div class="editor-label">
@Html.LabelFor(model => model.dept)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.dept)
@Html.ValidationMessageFor(model => model.dept)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.dept_phone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.dept_phone)
@Html.ValidationMessageFor(model => model.dept_phone)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
关于我做错了什么的任何想法?
更新
这是我的部门课程:
namespace CallCOP.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class Department
{
public int dept_id { get; set; }
public int contact_id { get; set; }
[DisplayName("Department")]
public string dept { get; set; }
[DisplayName("Phone Number")]
public string dept_phone { get; set; }
public virtual Contact Contact { get; set; }
}
}
而且,这是根据请求为我的 Create 视图呈现的 HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Create - My ASP.NET MVC Application</title>
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title"><a href="/">your logo here</a></p>
</div>
<div class="float-right">
<section id="login">
Hello, <span class="username">DOMAIN\userId</span>!
</section>
<nav>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/Home/About">About</a></li>
<li><a href="/Home/Contact">Contact</a></li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<section class="content-wrapper main-content clear-fix">
<form action="/Department/Create?contactId=1" method="post"><input name="__RequestVerificationToken" type="hidden" value="Q2GiIaT-qLIXxW_WN0o6o_5Tt5q2W6uXx6Ax2WEliVI2BqzAWFMZ7zwW0wX_R-rESzUn8mqFBChd8NB3jrZthK3d6LmmuRZQsdVu0v8hvSd_3_FstePrKvtjPXQ3BHtp3FXuqXdpduTsv2Nwzd1VI2cE16k1sq6QgL8ghP9HmXM1" /><input data-val="true" data-val-number="The field dept_id must be a number." data-val-required="The dept_id field is required." id="dept_id" name="dept_id" type="hidden" value="" /><input data-val="true" data-val-number="The field contact_id must be a number." data-val-required="The contact_id field is required." id="contact_id" name="contact_id" type="hidden" value="" /> <fieldset>
<legend>Department</legend>
<div class="editor-label">
<label for="dept">Department</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="dept" name="dept" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="dept" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="dept_phone">Phone Number</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="dept_phone" name="dept_phone" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="dept_phone" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
<div>
<a href="/Department">Back to List</a>
</div>
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© 2013 - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
</body>
</html>