我在尝试编辑时遇到问题。我与Areas合作以更好地管理应用程序。问题出在“管理员”这个区域。
接下来是控制器代码(OfficeControlle),我使用先前创建的会话变量和函数来编辑我得到的模型数据。
public ActionResult Edit()
{
decimal id;
id = (decimal)Session["consul"];
CAMPUS_UNIVERSITY campus_university = db. CAMPUS_UNIVERSITY.Single(s => s.Idoffice == id);
ViewData.Model = db.OFFICE.Single(c => c.Idoffice == id);
ViewBag.University = db.UNIVERSITY.Single(u => u.IdUniversity == campus_university.IdUniversity);
ViewBag.campus = db.CITY_CAMPUS.Single(u => u.IdCity == campus_university.Idcitycampus);
return View(sede_universidad);
}
[HttpPost]
public ActionResult Edit(CAMPUS_UNIVERSITY campus_university, OFFICE office)
{
if (ModelState.IsValid)
{
db.CAMPUS_UNIVERSITY.Attach(campus_university);
db.ObjectStateManager.ChangeObjectState(campus_university, EntityState.Modified);
db.SaveChanges();
db. OFFICE.Attach(office);
db.ObjectStateManager.ChangeObjectState(office, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.IdCitycampus = new SelectList(db.CITY_CAMPUS, "IdCity", "Name", campus_university.IdCitycampus);
ViewBag.IdConsultorio = new SelectList(db.OFFICE, "Idoffice", "addressoffice", campus_university.Idoffice);
ViewBag.IdUniversidad = new SelectList(db.UNIVERSITY, "IdUniversity", "Name", campus_university.IdUniversity);
return View(campus_university);
}
接下来是视图代码
@model RolMVC3.Models.CAMPUS_UNIVERSITY
@{
ViewBag.Title = "Edit";
}
<h2>edit</h2>
<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>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<h2> @ViewBag.University.Name - @ViewBag.Campus.Name </h2>
<fieldset>
<legend>OFFICE</legend>
@Html.HiddenFor(model => model.IdUniversity)
@Html.HiddenFor(model => model.IdCitycampus)
@Html.HiddenFor(model => model.Idoffice)
<div class="editor-label">
@Html.LabelFor(model => model.addresscampus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.addresscampus)
@Html.ValidationMessageFor(model => model.addresscampus)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.phonecampus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.phonecampus)
@Html.ValidationMessageFor(model => model.phonecampus)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.emailcampus)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.emailcampus)
@Html.ValidationMessageFor(model => model.emailcampus)
</div>
<fieldset>
<legend>OTHER DATE</legend>
@Html.Partial("_office", Model.OFFICE)
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("back", "Index")
</div>
当我按“保存”按钮时出现问题,出现以下错误:
参数字典包含非可空类型参数'Id'的空条目''System.Decimal'',用于''RolMVC3中的方法''System.Web.Mvc.ActionResult Index(System.Decimal)'。 Areas.Administrator.Controllers.OfficeController ''
答案 0 :(得分:2)
当你在这里重定向时:
return RedirectToAction("Index");
确保在查询字符串中传递id,因为看起来您的Index操作需要它作为参数:
return RedirectToAction("Index", new { id = campus_university.IdUniversity });
答案 1 :(得分:0)
我假设您的索引操作方法看起来像这样(如果我错了请纠正我):
public ActionResult Index(decimal id)
{
// Your method's code
}
因此,在您执行return RedirectToAction("Index");
的情况下,您正尝试重定向到不带参数的操作方法。但在这种情况下,有一个需要参数的动作方法,即id
。因此,在重定向时,您需要更改代码并提供此id parameter
。
这是你可以做的:
return RedirectToAction("Index", new { id = /* put your id here */ });