我正在使用MVC3创建一个网站,其中包含必须放入数据库的动态数据。但是我似乎无法找出为什么在我创建之后它不会附加到数据库中。
这是控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FYPMvcApplication.Models;
using System.Web.Providers.Entities;
namespace FYPMvcApplication.Controllers
{
public class CreateController : Controller
{
private bigModel db = new bigModel();
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Course course,LearningOut learningout)
{
var loc = new LearningOut();
loc.Outcomes = GetUserOutcomes();
if (ModelState.IsValid)
{
db.Courses.Add(course);
db.LearningOut.Add(learningout);
db.SaveChanges();
return RedirectToAction("Create", new { id = learningout.LearningOutID });
}
return View(loc);
}
private IList<LearningOut> GetUserOutcomes()
{
var outcomesList = new List<LearningOut>();
return outcomesList;
}
public ActionResult NewOutcomeRow(int id)
{
var outcome = new LearningOut { LearningOutID = id };
return View("partial/NewOutcomeRow", outcome);
}
}
}
型号:
public class LearningOut
{
public int LearningOutID { get; set; }
public string LearningOutcomes { get; set; }
public IList<LearningOut> Outcomes { get; set; }
public string LearningOutcomesText { get; set; }
public bool A { get; set; }
public bool B { get; set; }
public bool C { get; set; }
public bool D { get; set; }
public bool E { get; set; }
public bool F { get; set; }
public bool G { get; set; }
public bool H { get; set; }
public bool I { get; set; }
}
课程模型:
public class Course
{
public int CourseID { get; set; }
public string CourseCode { get; set; }
public string CourseName { get; set; }
public string ModuleCode { get; set; }
public string ModuleName { get; set; }
public int Year { get; set; }
public string Prerequisite { get; set; }
public string References { get; set; }
}
创建视图:
@model FYPMvcApplication.Models.mainModel
@{
ViewBag.Title = "Create";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnAdd").click(function (e) {
var itemIndex = $("#container input.iHidden").length;
e.preventDefault();
var newItem = $("<tr><td><input id='" + itemIndex + "' type='hidden' value='' class='iHidden' name='[" + itemIndex + "]' /><input type='text' id='" + itemIndex + "' name='[" + itemIndex + "]'/></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></<td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td><td><input type='checkbox' value='true' id='" + itemIndex + "' name='[" + itemIndex + "]' /></td></tr>");
$("#container").append(newItem);
});
});
</script>
<h2>Create</h2>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Step 1: Create Coruse & Module</a></li>
<li><a href="#tabs-2">Step 2: Instructional Outcomes</a></li>
<li><a href="#tabs-3">Step 3: Module Assessment Plan</a></li>
</ul>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<div id="tabs-1">
@* Course Code *@
<div class="editor-label">
@Html.LabelFor(model => model.course.CourseCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.course.CourseCode)
@Html.ValidationMessageFor(model => model.course.CourseCode)
</div>
@* Course Name *@
<div class="editor-label">
@Html.LabelFor(model => model.course.CourseName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.course.CourseName)
@Html.ValidationMessageFor(model => model.course.CourseName)
</div>
@* Module Code *@
<div class="editor-label">
@Html.LabelFor(model => model.course.ModuleCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.course.ModuleCode)
@Html.ValidationMessageFor(model => model.course.ModuleCode)
</div>
@* Module Name *@
<div class="editor-label">
@Html.LabelFor(model => model.course.ModuleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.course.ModuleName)
@Html.ValidationMessageFor(model => model.course.ModuleName)
</div>
@* Year *@
<div class="editor-label">
@Html.LabelFor(model => model.course.Year)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.course.Year)
@Html.ValidationMessageFor(model => model.course.Year)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.course.Prerequisite)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.course.Prerequisite)
@Html.ValidationMessageFor(model => model.course.Prerequisite)
</div>
<br />
<div class="editor-field">
<table id="container">
<tr>
<th>Learning Outcomes</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
</tr>
@Html.EditorFor(model => model.learningOut.Outcomes)
</table>
<input type="button" id="btnAdd" value="Add New Outcome" />
</div>
<div class="editor-label">
@Html.LabelFor(model => model.course.References)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.course.References)
@Html.ValidationMessageFor(model => model.course.References)
</div>
</div>
<div id="tabs-2">
- Construction in progress -
</div>
<div id="tabs-3">
<input type="submit" value="Create" />
</div>
</fieldset>
}
</div>
编辑。
我的EditorTemplate视图
@model FYPMvcApplication.Models.mainModel
@{
Layout = null;
}
<tr>
<td>
@Html.HiddenFor(x => x.learningOut.LearningOutID, new { @class = "iHidden" })
@Html.EditorFor(x => x.learningOut.LearningOutcomesText) </td>
<td>@Html.EditorFor(x => x.learningOut.Outcomes)
@Html.EditorFor(x => x.learningOut.A)
@Html.EditorFor(x => x.learningOut.B)
@Html.EditorFor(x => x.learningOut.C)
@Html.EditorFor(x => x.learningOut.D)
@Html.EditorFor(x => x.learningOut.E)
@Html.EditorFor(x => x.learningOut.F)
@Html.EditorFor(x => x.learningOut.G)
@Html.EditorFor(x => x.learningOut.H)
@Html.EditorFor(x => x.learningOut.I) </td>
</tr>
部分视图
@model FYPMvcApplication.Models.mainModel
@{ Layout = null; }
<tr>
<td>
<input type="hidden" id="@(Model.learningOut.LearningOutID)_id" class="iHidden" name='[@Model.learningOut.LearningOutID].id' />
<input type='text' id='@(Model.learningOut.LearningOutID)_text' name='[@Model.learningOut.LearningOutID].text' />
</td>
<td>
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_A' name='[@Model.learningOut.LearningOutID].A'/>
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_B' name='[@Model.learningOut.LearningOutID].B' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_C' name='[@Model.learningOut.LearningOutID].C' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_D' name='[@Model.learningOut.LearningOutID].D' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_E' name='[@Model.learningOut.LearningOutID].E' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_F' name='[@Model.learningOut.LearningOutID].F' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_G' name='[@Model.learningOut.LearningOutID].G' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_H' name='[@Model.learningOut.LearningOutID].H' />
<input type='checkbox' value='true' id='@(Model.learningOut.LearningOutID)_I' name='[@Model.learningOut.LearningOutID].I' />
</td>
</tr>
这是问题所在。我的课程模型似乎能够出现在我的数据库中。但我的学习不能。
编辑:
mainModel查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace FYPMvcApplication.Models
{
public class mainModel
{
public Course course { get; set; }
public LearningOut learningOut { get; set; }
}
}
包括mainmodel&amp;更新了我的创建视图。
答案 0 :(得分:0)
我认为,您可以将控制器更改为..
sub my_sub {
my $v1 = shift;
my $v2 = shift;
print "The value of the first passed variable is: $v1";
print "The value of the first passed variable is: $v1";
}