Reputation: 93
I have a problem trying to edit. I work with Areas for better management the application.The problem is in the areas called "Administrator".
Next is the controller code (OfficeControlle), I use a session variable has been previously created and functions to edit the model data I get.
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);
}
Next is the view code
@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>
The problem appears when I press the "Save" button, get the following error:
The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type ''System.Decimal' ' for method ''System.Web.Mvc.ActionResult Index(System.Decimal)' in ''RolMVC3.Areas.Administrator.Controllers.OfficeController''
Upvotes: 0
Views: 316
Reputation: 26048
I'm assuming that your Index action method looks something like this (please correct me if I am wrong):
public ActionResult Index(decimal id)
{
// Your method's code
}
So where you do return RedirectToAction("Index");
you are trying to redirect to an action method that takes no parameters. But in this case there is an action method that requires a parameter, namely id
. So when redirecting you need to change your code and supply this id parameter
.
This is what you could do:
return RedirectToAction("Index", new { id = /* put your id here */ });
Upvotes: 0
Reputation: 1039248
When you are redirecting here:
return RedirectToAction("Index");
make sure that you pass the id in the query string as it looks like your Index action requires it as parameter:
return RedirectToAction("Index", new { id = campus_university.IdUniversity });
Upvotes: 2