Reputation: 89
I have a form that is fully complete by a model but I cannot get some attributes because I believe that in this moment the model is null. The thing is that when I use @Model.someattribute
it doesn't work, but with m => m.cantidadMensajes
, it does. I need to know why it happened and how I have to deal with it?
@model SeaConnectionManager.Modelos.EnvioMensajes.InformacionEntity
@{ var previsualizaciones = "";
if (@Model != null) {
previsualizaciones = @Model.vistaPrevia;
}
}
<div id="Envio" class="overlay">
<div class="popup">
<h2 align="center">Información del Envío</h2>
<a class="close" href="#">×</a>
<div class="content" style="width:100%; margin:auto; padding:10px">
<center>
<a id="tabs">
<ul>
@if (@Model != null) {
if (@Model.cantidadMensajes >= 5)
{
for (var i = 1; i <= 5; i++)
{
<li><a href="#tabs-@i"> @i</a></li>
}
}
else {
for (var i = 1; i <= @Model.cantidadMensajes; i++)
{
<li><a href="#tabs-@i"> @i</a></li>
}
}
}
</ul>
@previsualizaciones
</div>
<table width="100%" border="0" align="center" cellspacing="4">
<tr>
<td width="36%">Cantidad de mensajes</td>
<td width="44%">
@Html.TextBoxFor(m => m.cantidadMensajes, new { @id = "CantidadMensajes", @class = "input-login", @readonly = "readonly" })
</td>
</tr>
<tr>
<td>Cantidad de contactos</td>
<td>
@Html.TextBoxFor(m => m.cantidadContactos, new { @id = "CantidadContactos", @class = "input-login", @readonly = "readonly" })
</td>
</tr>
<tr>
<td>Saldo</td>
<td>
@Html.TextBoxFor(m => m.saldo, new { @id = "Saldo", @class = "input-login", @readonly = "readonly" })
@Html.HiddenFor(m => m.idTransaccion, new { @id = "idTransaccion" })
</td>
</tr>
</table>
</center><br>
<center>
<table width="122" border="0" align="center" cellspacing="10">
<tr>
<td width="51"><a onclick="EnviarMensajes()" class="boton-para-enviar" style="margin:auto">Aprobar</a></td>
<td width="37"><a href="#" class="boton-para-NO-enviar" style="margin:auto">Rechazar</a></td>
</tr>
</table>
</center>
Changes after following some advices with no luck:
@model SeaConnectionManager.Modelos.EnvioMensajes.InformacionEntity
@{ var previsualizaciones = "";
if (Model != null) {
previsualizaciones = Model.vistaPrevia;
}
}
<div id="Envio" class="overlay">
<div class="popup">
<h2 align="center">Información del Envío</h2>
<a class="close" href="#">×</a>
<div class="content" style="width:100%; margin:auto; padding:10px">
<center>
<a id="tabs">
<ul>
@if (Model != null) {
if (Model.cantidadMensajes >= 5)
{
for (var i = 1; i <= 5; i++)
{
<li><a href="#tabs-@i"> @i</a></li>
}
}
else {
for (var i = 1; i <= Model.cantidadMensajes; i++)
{
<li><a href="#tabs-@i"> @i</a></li>
}
}
}
</ul>
@previsualizaciones
</div>
<table width="100%" border="0" align="center" cellspacing="4">
<tr>
<td width="36%">Cantidad de mensajes</td>
<td width="44%">
@Html.TextBoxFor(m => m.cantidadMensajes, new { @id = "CantidadMensajes", @class = "input-login", @readonly = "readonly" })
</td>
</tr>
<tr>
<td>Cantidad de contactos</td>
<td>
@Html.TextBoxFor(m => m.cantidadContactos, new { @id = "CantidadContactos", @class = "input-login", @readonly = "readonly" })
</td>
</tr>
<tr>
<td>Saldo</td>
<td>
@Html.TextBoxFor(m => m.saldo, new { @id = "Saldo", @class = "input-login", @readonly = "readonly" })
@Html.HiddenFor(m => m.idTransaccion, new { @id = "idTransaccion" })
</td>
</tr>
</table>
</center><br>
<center>
<table width="122" border="0" align="center" cellspacing="10">
<tr>
<td width="51"><a onclick="EnviarMensajes()" class="boton-para-enviar" style="margin:auto">Aprobar</a></td>
<td width="37"><a href="#" class="boton-para-NO-enviar" style="margin:auto">Rechazar</a></td>
</tr>
</table>
</center>
<br>
<strong>Nota:</strong> Si el saldo no se encuentra completo para la cantidad total de mensajes, los mensajes que queden pendientes se enviarán cuando realice la recarga.
</div>
</div>
</div>
Check the controller out:
public ActionResult EnviarMensajeIndividual(MensajesEntity model)
{
InformacionEntity Info = new InformacionEntity();
Info.error = false;
model.usuarioCreacion = User.Identity.Name;
if (HttpContext.Session["Lista"] != null && model.mensaje != null)
{
List<ContactoEntity> lista = HttpContext.Session["Lista"] as List<ContactoEntity>;
int idTransaccion = mdm.EnviarMensajes(model, lista, 1);
if (idTransaccion > 0)
{
Info = mdm.InformacionDeEnvio(idTransaccion);
Info.error = false;
}
else
{
//error
Info.error = true;
Info.mensajeError = "error.";
}
}
else
{
// tiene q tener contactos agregados
Info.error = true;
Info.mensajeError = "error";
}
var js = new JavaScriptSerializer();
var Data = new ContentResult();
js.MaxJsonLength = Int32.MaxValue;
Data.Content = js.Serialize(Info);
Data.ContentType = "application/json";
return Data;
}
Upvotes: 0
Views: 1044
Reputation: 6891
Model type of your view is SeaConnectionManager.Modelos.EnvioMensajes.InformacionEntity
That means the view should be rendered with an object of that type.
The controller action of yours show that you are not returning the view at all. Instead you are returning ContentResult. The content result can not be translated to the model type expected by the view. That's why your model is always null and you are not getting inside the block "if (Model != null)".
You need to make following changes to the controller action.
public ActionResult EnviarMensajeIndividual(MensajesEntity model)
{
InformacionEntity Info = new InformacionEntity();
Info.error = false;
model.usuarioCreacion = User.Identity.Name;
if (HttpContext.Session["Lista"] != null && model.mensaje != null)
{
List<ContactoEntity> lista = HttpContext.Session["Lista"] as List<ContactoEntity>;
int idTransaccion = mdm.EnviarMensajes(model, lista, 1);
if (idTransaccion > 0)
{
Info = mdm.InformacionDeEnvio(idTransaccion);
Info.error = false;
}
else
{
//error
Info.error = true;
Info.mensajeError = "error.";
}
}
else
{
// tiene q tener contactos agregados
Info.error = true;
Info.mensajeError = "error";
}
return View(Info);
}
Here I am assuming that the name of your view is the same as the name of your action.
Upvotes: 1
Reputation: 5943
The reason why your Model is being given the value of null is because at the top of your view you have this:
@model SeaConnectionManager.Modelos.EnvioMensajes.InformacionEntity
So, your view is expecting an object of type InformacionEntity
.
However in your controller where you are supposed to be returning that object to the view, you are not returning an InformacionEntity
object.
So in your controller, you have this:
InformacionEntity Info = new InformacionEntity();
Which is what you need to return.
So change your return statement to:
return View(Info);
Let me know if this helps!
Upvotes: 1
Reputation: 131
Remove @ from Model
@if (Model != null) {
if (Model.cantidadMensajes >= 5)
{
for (var i = 1; i <= 5; i++)
{
<li><a href="#tabs-@i"> @i</a></li>
}
}
else {
for (var i = 1; i <= Model.cantidadMensajes; i++)
{
<li><a href="#tabs-@i"> i</a></li>
}
}
}
Upvotes: 1