My Clock class could have many ClockAction objects related to it. My view edit is using the ManageClocks controller so it can edit both Clock and ClockAction.
The edit view should allow to edit all ClockActions for a particulair Clock. The ClockActions will have a custom field name, ie "Update Firmware". Checking off the checkbox for this should create a ClockActions object with a specified clockType (based on the
field).
My edit view will edit all the properties in the Clock model, but how do I create a ClockAction for each input field? Also, how do I specified clockType in the background per field?
public class Clock
{
public int ID { get; set; }
public string SerialNumber { get; set; }
public string FirmwareVersion { get; set; }
public DateTime? LastActivityDate { get; set; }
public virtual ICollection<ClockAction> ClockAction{ get; set; }
}
public class ClockAction
{
public int ID { get; set; }
public int ClockID { get; set; }
[ForeignKey("ClockID")]
public virtual Clock Clock { get; set; }
public string ActionCommandString { get; set; }
public int ActionType { get; set; }
}
public class ManageClocks
{
public Clock ClockManageModel { get; set; }
public ClockAction ClockAction { get; set; }
public void ClockAndClockActionsView(Clock clockManageModel, ClockAction clockAction)
{
ClockManageModel = clockManageModel;
ClockAction = clockAction;
}
}
Edit View
@model ClockServer.Models.ManageClocks
@{
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>Clock</legend>
@Html.HiddenFor(model => model.ClockManageModel.ID)
<div class="editor-label">
@Html.LabelFor(model => model.ClockManageModel.SerialNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ClockManageModel.SerialNumber)
@Html.ValidationMessageFor(model => model.ClockManageModel.SerialNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ClockManageModel.FirmwareVersion)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ClockManageModel.FirmwareVersion)
@Html.ValidationMessageFor(model => model.ClockManageModel.FirmwareVersion)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ClockManageModel.LastActivityDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ClockManageModel.LastActivityDate)
@Html.ValidationMessageFor(model => model.ClockManageModel.LastActivityDate)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
I changed my ManageClocks model to have a property which contains a collection of ClockActions. The only problem with the method described in the link is that when I pass the ManageClocks model to the edit view, it will not recoginize the ClockActions
property as a collection. It will recognize model.ClockAction but not model.ClockAction[i].<property name>.
public class ManageClocks
{
public Clock ClockManageModel { get; set; }
public ICollection<ClockAction> ClockAction { get; set; }
}
public ActionResult Edit(int id)
{
ManageClocks manageClocks = new ManageClocks();
manageClocks.ClockManageModel = db.Clocks.Find(id);
return View(manageClocks);
}
gehrig44
Member
4 Points
10 Posts
MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 12:16 AM|LINK
My Clock class could have many ClockAction objects related to it. My view edit is using the ManageClocks controller so it can edit both Clock and ClockAction.
The edit view should allow to edit all ClockActions for a particulair Clock. The ClockActions will have a custom field name, ie "Update Firmware". Checking off the checkbox for this should create a ClockActions object with a specified clockType (based on the field).
My edit view will edit all the properties in the Clock model, but how do I create a ClockAction for each input field? Also, how do I specified clockType in the background per field?
public class Clock { public int ID { get; set; } public string SerialNumber { get; set; } public string FirmwareVersion { get; set; } public DateTime? LastActivityDate { get; set; } public virtual ICollection<ClockAction> ClockAction{ get; set; } } public class ClockAction { public int ID { get; set; } public int ClockID { get; set; } [ForeignKey("ClockID")] public virtual Clock Clock { get; set; } public string ActionCommandString { get; set; } public int ActionType { get; set; } } public class ManageClocks { public Clock ClockManageModel { get; set; } public ClockAction ClockAction { get; set; } public void ClockAndClockActionsView(Clock clockManageModel, ClockAction clockAction) { ClockManageModel = clockManageModel; ClockAction = clockAction; } } Edit View @model ClockServer.Models.ManageClocks @{ 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>Clock</legend> @Html.HiddenFor(model => model.ClockManageModel.ID) <div class="editor-label"> @Html.LabelFor(model => model.ClockManageModel.SerialNumber) </div> <div class="editor-field"> @Html.EditorFor(model => model.ClockManageModel.SerialNumber) @Html.ValidationMessageFor(model => model.ClockManageModel.SerialNumber) </div> <div class="editor-label"> @Html.LabelFor(model => model.ClockManageModel.FirmwareVersion) </div> <div class="editor-field"> @Html.EditorFor(model => model.ClockManageModel.FirmwareVersion) @Html.ValidationMessageFor(model => model.ClockManageModel.FirmwareVersion) </div> <div class="editor-label"> @Html.LabelFor(model => model.ClockManageModel.LastActivityDate) </div> <div class="editor-field"> @Html.EditorFor(model => model.ClockManageModel.LastActivityDate) @Html.ValidationMessageFor(model => model.ClockManageModel.LastActivityDate) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>ignatandrei
All-Star
135210 Points
21690 Posts
Moderator
MVP
Re: MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 02:40 AM|LINK
See http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
gehrig44
Member
4 Points
10 Posts
Re: MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 02:03 PM|LINK
Thanks for the link.
I changed my ManageClocks model to have a property which contains a collection of ClockActions. The only problem with the method described in the link is that when I pass the ManageClocks model to the edit view, it will not recoginize the ClockActions property as a collection. It will recognize model.ClockAction but not model.ClockAction[i].<property name>.
public class ManageClocks { public Clock ClockManageModel { get; set; } public ICollection<ClockAction> ClockAction { get; set; } } public ActionResult Edit(int id) { ManageClocks manageClocks = new ManageClocks(); manageClocks.ClockManageModel = db.Clocks.Find(id); return View(manageClocks); }Any way around this?
ignatandrei
All-Star
135210 Points
21690 Posts
Moderator
MVP
Re: MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 02:12 PM|LINK
Instead of
gehrig44
Member
4 Points
10 Posts
Re: MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 06:40 PM|LINK
Ah.. thanks a bunch. I have it working now!
CPrakash82
All-Star
18314 Points
2851 Posts
Re: MVC3 - How to edit multiple instances of same model in edit view?
Jun 29, 2012 11:11 PM|LINK
Please mark the correct reply as answer it will be helpful for others.
Thanks,