I have a MVC application. I am having an issue with the Edit method. It is not posting back to the database. I added breakpoints and stepped through line by line and the value seems to update but it is not updating database. The database does not have a primary id field but the other fields (username,service, short_plan, role) are all PK. Ideas?
Model:
[MetadataType(typeof(Department_RolesMetadata))]
public partial class Department_Roles
{
}
public class Department_RolesMetadata
{
[Required]
public string username { get; set; }
[Required]
public string service { get; set; }
[Required]
public string short_plan { get; set; }
[Required]
public string role { get; set; }
}
Controller:
public ActionResult Edit(string username, string service, string sp, string role)
{
Department_Roles department_roles = db.Department_Roles.Where(dr => dr.username == username && dr.service == service && dr.short_plan == sp && dr.role == role).First();
ViewBag.username = new SelectList(db.Service_Logins, "username", "user_type", department_roles.username);
return View(department_roles);
}
[HttpPost]
public ActionResult Edit(Department_Roles department_roles)
{
if (ModelState.IsValid)
{
db.Department_Roles.Attach(department_roles);
db.Entry(department_roles).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.username = new SelectList(db.Service_Logins, "username", "user_type", department_roles.username);
return View(department_roles);
}
View:
@model NS.Models.Department_Roles
@{
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)
<fieldset>
<legend>Department</legend>
<div class="editor-label">
@Html.LabelFor(model => model.username)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.username,NS.Models.Department_Roles.GetServiceLogins(),"--Select One--")
@Html.ValidationMessageFor(model => model.username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.service)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.service,NS.Models.Service_Logins.GetUserCompetitionTypes(),"--Select One--")
@Html.ValidationMessageFor(model => model.service)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.short_plan)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.short_plan,NS.Models.FeeAuth.GetActiveShortPlans(),"--Select One--")
@Html.ValidationMessageFor(model => model.short_plan)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.role)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.role)
@Html.HiddenFor(model => model.role)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "IndividualIndex", new { username = Model.username })
</div>