1
below 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 registration and an email registration:
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 Edit view of the email page, I would like to bring the name of the client (owner of the email), but not so that the user can edit it but just so he can be sure which email he 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 on Save, if Modelstate.Isvalid is false, this page will come back with the empty Client name, since the page is filled with the data of the last sent form and this field (Customer name) is not part of the Model Email, but part of the Model Client.
How do you suggest I solve this problem?
Nice to meet you, LINQ! I thought about doing with Viewbag myself, but I was not really liking the idea, because on other pages I will have several of these fields to fill and I would have to change my View a lot. Now that first option worked well! Thank you!
– Rafael