【发布时间】:2015-02-12 22:50:10
【问题描述】:
我对 MVC 和 Razor 还很陌生(是的,真丢脸), 我有一个包含 2 个部分视图和 1 个 ajax 表单的页面,其中一个部分视图具有动态生成的列表更新值(并且失败)到另一个部分视图,根据设计它显示列表的第一条消息。
问题是当我按下列表中的一项(“_ListaMensajes”中的“Ver”)上的按钮时,而不是在页面底部显示消息的详细信息(_detalleMensaje),它什么也没做(没有在控制器调用,或任何东西)
我遵循了几个教程和工作项目来做到这一点,这让我很困惑,我不知道这段代码是否有意义,请帮忙。
谢谢
PD:我正在使用 MVC 5.1(根据 nuget 控制台) 并引用:jquery.unobtrusive-ajax.min.js
控制器:
public ActionResult DetalleMensaje(int Id_Mensaje) {
V_Sedd_Mensaje oV_Sedd_Mensaje = oI_V_Sedd_Mensaje.Recuperar_V_Sedd_Mensaje_PorCodigo(Id_Mensaje);
return PartialView("_DetalleMensaje", oV_Sedd_Mensaje);
}
页面:
<div class="col-md-10">
<fieldset class="dhhBorder">
<legend class="dhhBorder">Lista de Envíos</legend>
<div class="form-group">
<div class="row">
<div class="col-lg-1">Evento</div>
<div class="col-lg-2">@Html.DisplayFor(x => x.Descripcion)</div>
<div class="col-lg-1">Fecha Envio</div>
<div class="col-lg-1">@Html.DisplayFor(x => x.Fecha_Envio_Short)</div>
<div class="col-lg-1">
<button type="button" title="Editar" data-url="/Envios/VerCuadros?Id_Envio_Empresa=@Html.DisplayFor(x => x.Id_Envio_Empresa)" class="btn btn-default btn-xs modalMTC"><span class="glyphicon glyphicon-paperclip"></span></button>
</div>
</div>
<div class="row">
@using (Ajax.BeginForm("DetalleMensaje", "Envios", new { @id = "FormCabecera" },
new AjaxOptions()
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "SeccionMensaje"
}))
{
<div class="col-md-10">
<fieldset class="dhhBorder">
<legend class="dhhBorder">Lista de Envíos</legend>
<div class="row">
<div class="col-md-12" id="SeccionListado">
@Html.Partial("_ListaMensajes", @Model.lV_Sedd_Mensaje)
</div>
</div>
</fieldset>
</div>
}
<div class="col-md-10" id="SeccionMensaje">
@Html.Partial("_DetalleMensaje", Model.oV_Sedd_Mensaje)
</div>
</div>
</div>
</fieldset>
</div>
_ListaMensajes:
@using aseDGRAIC.Helpers
@model List<beDGRAIC.V_Sedd_Mensaje>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover table-responsive">
<thead class="small">
<tr class="text-center">
<th class="text-center col-lg-1" scope="col">Orden</th>
<th class="text-center col-lg-2" scope="col">Tipo</th>
<th class="text-center col-lg-4" scope="col">Asunto</th>
<th class="text-center col-lg-1" scope="col">Adjunto</th>
<th class="text-center col-lg-2" scope="col">Fecha / Hora</th>
<th class="text-center col-lg-2" scope="col">Acciones</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
var cuenta = 1;
foreach (var x in Model)
{
<tr>
<td class="text-center">@cuenta</td>
<td>@x.Tipo</td>
<td>@x.Asunto</td>
@if (x.Existen_Adjuntos != 0) {
<td><button type="button" title="Editar" data-url="/Envios/VerAdjuntos?Id_Mensaje=@x.Id_Mensaje" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-paperclip"></span></button></td>
} else {
<td> </td>
}
<td>@x.Fecha</td>
<td class="text-center">
<button type="submit" title="Ver" data-url="/Envios/DetalleMensaje?Id_Mensaje=@x.Id_Mensaje" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-comment"></span></button>
</td>
</tr>
}
}
</tbody>
</table>
</div>
和_detalleMensaje:
@model beDGRAIC.V_Sedd_Mensaje
<div class="panel">
<div class="row">
<div class="col-md-12">
<div class="row form-horizontal">
<div class="col-sm-6 col-md-6">
<div class="form-group">
<label class="col-md-3 control-label small">Asunto:</label>
<div class="col-md-9">
@if (Model.Id_Mensaje != 0)
{
@Html.TextBoxFor(x => x.Asunto, new { @class = "form-control input-sm", @disabled = "disabled" })
}
else
{
@Html.TextBoxFor(x => x.Asunto, new { @class = "form-control input-sm" })
}
</div>
</div>
</div>
</div>
<div class="row form-horizontal">
<div class="col-sm-6 col-md-6">
<div class="form-group">
<label class="col-md-3 control-label small">Mensaje:</label><br />
<div class="col-md-9">
@if (Model.Id_Mensaje != 0)
{
@Html.TextBoxFor(x => x.Mensaje, new { @class = "form-control input-sm", @disabled = "disabled" })
}
else
{
@Html.TextBoxFor(x => x.Mensaje, new { @class = "form-control input-sm" })
}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="panel-footer">
<div class="btn-group">
@if (Model.Id_Mensaje == 0) {
<button id="btnGrabar" type="button" class="btn btn-success btn-sm">Enviar</button>
}
</div>
</div>
编辑:根据下面的 cmets 对代码进行了一些更改,但行为相同
【问题讨论】:
-
我的布局中的错误是否会干扰 jquery?在:
$(window).on('load', function () { $('.selectpicker').selectpicker(); <--- here if (globalLog == '0') { $('.toggleButtonMenu').click(); } });我得到:“Uncaught TypeError: undefined is not a function” -
尝试了不同的布局而没有错误并且行为方式相同......
-
您有一个 ajax 表单,但没有提交按钮。您在表单中的部分 (
_ListaMensajes) 不会呈现任何控件,因此无论如何都不会提交任何内容。而且您循环正在生成无效的html(重复的idattributes). Then your second partial_detalleMensaje`,它确实有一些控件在表单之外,所以不清楚您在这里实际尝试做什么。 -
感谢您的观察,我将解释我正在尝试做的事情 On _ListaMensajes,仅用于选择要显示的消息(当按下“Ver”按钮时 - 我猜它必须在 _DetlleMensaje partialview 上成为 submit-)。对于第二部分,我假设将通过控制器方法传递模型来填充控件,该方法在 Ajax 表单上引用
标签: jquery ajax asp.net-mvc model-view-controller ajax.beginform