【发布时间】:2017-01-25 19:35:41
【问题描述】:
我有一个完全由模型完成的表单,但我无法获得某些属性,因为我相信此时模型为空。问题是,当我使用 @Model.someattribute 时它不起作用,但使用 m => m.cantidadMensajes 时,它会起作用。我需要知道它发生的原因以及我必须如何处理它?
@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>
在没有运气的情况下遵循一些建议后的变化:
@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>
检查控制器:
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;
}
【问题讨论】:
-
如果
@符号在if语句之前,则不需要在Model之前使用@符号。 -
由于您的视图在顶部有这个..
@model SeaConnectionManager.Modelos.EnvioMensajes.InformacionEntity.. 它需要一个InformacionEntity.. 类型的对象,所以你必须给它那个对象.. 在你的控制器中是InformacionEntity Info = new InformacionEntity();.. 所以return View(Info); -
您发布的操作甚至不会返回视图,因此如果您走得足够远以至于看到
Model在您的视图中为空,那么它显然不是正确的。 -
属性不是属性。请不要混淆两者。
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor