Using the Entity Data Framework, I have a model defined called Client
which simply contains an id and a name. I then have an association with another model called Appointment
containing an id and a date.
For the form, I wish to allow users to create a new client and on the same form, add appointments (before the client is created). I have it partially working (adding one appointment).
In the controller:
[HttpPost]
public ActionResult Create(Client client)
{
var appointment = new Appointment();
UpdateModel(appointment);
client.Appointments.Add(appointment);
db.AddToClients(client);
db.SaveChanges();
return RedirectToAction("Index");
}
In the view:
<div class="editor-label">
<%= Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Name) %>
<%= Html.ValidationMessageFor(model => model.Name) %>
</div>
<% Html.RenderPartial("Appointment", new Appointment()); %>
'Partial' view:
<div class="editor-label">
<%= Html.LabelFor(model => model.AppointmentDate) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.AppointmentDate) %>
<%= Html.ValidationMessageFor(model => model.AppointmentDate) %>
</div>
However, this is not ideal for several reasons:
- Only one appointment can be added
- Issues will occur if there are any name conflicts (e.g. a field called
Comments
in bothClient
andAppointment
table), since no prefixing is done on the field names, which also prevents more than one appointment being added as well
What can I do to allow multiple appointments to be added, i.e. using a button to render a partial view (using AJAX probably) on the page when 'new appointment' is clicked to allow as many appointments as possible? I would also like this to be a shared view that can be used for both creating and editing records as well as adding/deleting appointments in the same form.