Below is my question if you can help. I use C # ASP.NET MVC 5 with Entity Framework.
I have classes that represent below respectively a customer registry and an e-mail registry:
public partial class Cliente
{
[Key]
public int ClienteId { get; set; }
public string Nome { get; set; }
public virtual ICollection<Email> { get; set; }
}
public partial class Email
{
[Key]
public int EmailId { get; set; }
public string Email { get; set; }
public int ClienteId { get; set; }
public virtual Cliente { get; set; }
}
When I create the view of the email edit page, I would like to bring the client's name (owner of the email), but not so that the user can edit it, but only so that he is sure about which email it is changing.
So, in the view I do something like:
@model Models.Cadastro.Email
//[...]
<div class="form-group">
@Html.LabelFor(model => model.Cliente.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10 txt_disable">
@Html.ValueFor(model => model.Cliente.Nome)
</div>
</div>
//[...]
So far everything works perfect, the problem is that when I click Save, if ModelState.IsValid is false, this page will return with the Customer Name blank, since the page is populated with the data from the last form and this field (Customer Name) is not part of Model Email and yes Customer Model.
What form do you suggest for me to solve this problem?