I have a schedule class where I need to save the client data, the time, and the service that will be scheduled. However, the service is saving correctly through a selection in the DropDownList, but the client data and the Schedule are not saving in the schedule, only in the Client and Schedule classes.
How should I do it? Does it solve with a ViewModel?
Here are code for classes, controllers, and Views:
Schedule Action Create:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID, IDServico, IDCliente, IDHorario" )] Agenda agenda, Cliente cliente, Horario horario)
{
if (ModelState.IsValid)
{
db.Clientes.Add(cliente);
db.Horarios.Add(horario);
db.Agendas.Add(agenda);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.IDServico = new SelectList(db.Servicos, "ID", "Nome", agenda.IDServico);
ViewBag.IDCliente = new SelectList(db.Clientes, "ID", "Nome", agenda.IDCliente);
ViewBag.IDHorario = new SelectList(db.Horarios, "ID", "Horario", agenda.IDHorario);
return View(agenda);
}
Calendar view Create:
@model Barbearia.Models.Agenda
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Agenda</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.IDCliente, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cliente.Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IDCliente, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Cliente.Telefone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cliente.Telefone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Cliente.Telefone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Cliente.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cliente.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Cliente.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Horario.Data, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Horario.Data, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Horario.Data, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Horario.Hora, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Horario.Hora, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Horario.Hora, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IDServico, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.IDServico, null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IDServico, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Schedule class:
'using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Barbearia.Models
{
public class Agenda
{
[Key]
public int ID { get; set; }
//chaves extrangeiras
[Display(Name = "Serviços")]
public int IDServico { get; set; }
public virtual Servico Servico { get; set; }
[Display(Name = "Cliente")]
public int IDCliente { get; set; }
public virtual Cliente Cliente { get; set; }
public int IDHorario { get; set; }
public virtual Horario Horario { get; set; }
}
}'
Hug.