好的我经过几个小时的故障排除后放弃了,但我相信你们都会马上看到解决方案。我有三个模型Person,Course和CoursePreference。 CoursePreference模型有两个外键PersonId和CourseId。
我想做什么: 我想创建一个视图,用户可以在顶部的Create部分中添加课程首选项,当他们单击Add时,表单将在同一视图中发布和刷新List。基本上我在一个视图中组合索引和创建。所以我在CoursePreference文件夹中创建了一个Index View和一个名为_CreatePartial的局部视图。
问题: 视图显示正常,但有两个问题。 1)CoursePreferenceId字段显示下拉列表。我希望它是一个隐藏的领域,因为它是一个身份。我完全从脚手架的Create View中复制了代码,它正确地隐藏了Id。不知道为什么它不能在_CreatePartial视图中工作? 2)最重要的是,我的_CreatePartial不会添加任何课程偏好。看起来好像表单正在发布但没有添加记录。
是什么给出了?
以下是模型,控制器和视图:
---------------------------
Models (stripped down versions)
---------------------------
public class CoursePreference
{
public int CoursePreferenceId { get; set; }
public Nullable<int> CourseId { get; set; }
public Nullable<int> PersonId { get; set; }
public virtual Course Course { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<CoursePreference> CoursePreferences { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public Nullable<int> ProgramId { get; set; }
public string Name { get; set; }
public virtual ICollection<CoursePreference> CoursePreferences { get; set; }
}
------------------
Controllers
------------------
public ActionResult _CreatePartial()
{
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
ViewBag.PersonId = new SelectList(db.People, "PersonId", "LastName");
return View("_CreatePartial");
}
public ActionResult Index()
{
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
ViewBag.PersonId = new SelectList(db.People, "PersonId", "LastName");
var coursepreferences = db.CoursePreferences.Include(c => c.Course).Include(c => c.Person);
return View(coursepreferences.ToList());
}
---------------------------
Index View
---------------------------
@model IEnumerable<MyProj.Models.CoursePreference>
@{ ViewBag.Title = "Index";
}
<h4>Add Course Preferences</h4>
<div>
@Html.Partial("~/Views/CoursePreference/_CreatePartial.cshtml", new MyProj.Models.CoursePreference())
</div>
<br />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Course.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Person.LastName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Course.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Person.LastName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CoursePreferenceId }) |
@Html.ActionLink("Details", "Details", new { id=item.CoursePreferenceId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CoursePreferenceId })
</td>
</tr>
}
</table>
---------------------------
_CreatePartial View
---------------------------
@model MyProj.Models.CoursePreference
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CoursePreference</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.CoursePreferenceId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CoursePreferenceId)
@Html.ValidationMessageFor(model => model.CoursePreferenceId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CourseId, "CourseId", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CourseId", String.Empty)
@Html.ValidationMessageFor(model => model.CourseId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PersonId, "PersonId", new { @class = "control-label col-md-2" }
<div class="col-md-10">
@Html.DropDownList("PersonId", String.Empty)
@Html.ValidationMessageFor(model => model.PersonId)
</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>
答案 0 :(得分:1)
由于您没有控制器方法来处理帖子,因此无法保存任何内容。在您的控制器中实现:
[HttpPost]
public ActionResult Index(CoursePreference pref)
{
// Insert code to fetch database context into variable "db"
db.CoursePreferences.Add(pref);
db.SaveChanges();
return RedirectToAction("Index");
}
对于隐藏的ID字段,您应该使用:
@Html.HiddenFor(model => model.CoursePreferenceId)
-not EditorFor
或LabelFor
。