【发布时间】:2011-08-15 13:19:59
【问题描述】:
我有一个包含多个 PartialViews 的 ASP.NET MVC 3 视图。其中一个从 Internet 获取图像,该图像取决于其 ViewModel 的参数。我想更改 ViewModel 的参数并重新加载 PartialView(仅),以便加载更改参数的图像。
到目前为止,这是我的 PartialView:
@model MyViewModel
<script type="text/javascript">
function changeParameter(newValue) {
@Model.Parameter = newValue;
alert(@Model.Parameter);
... What now? How do I refresh the PartialView? ...
}
</script>
<h4>@Model.Header</h4>
<table style="background-color: #f8f7f8; width:100%;">
<tr style="line-height:18px; background-color: #d6d6d6;">
<th style="cursor:pointer" onclick="changeParameter('value1');">Value1</th>
<th style="cursor:pointer" onclick="changeParameter('value2');">Value2</th>
<th style="cursor:pointer" onclick="changeParameter('value3');">Value3</th>
<th style="cursor:pointer" onclick="changeParameter('value4');">Value4</th>
<th style="cursor:pointer" onclick="changeParameter('value5');">Value5</th>
<th style="cursor:pointer" onclick="changeParameter('value6');">Value6</th>
<th style="cursor:pointer" onclick="changeParameter('value7');">Value7</th>
</tr>
</table>
<img src="@Model.ImageUrl" alt="Chart" />
所以问题是我想单击其中一个值 (Value1-7) 并且我的 ViewModel 的 Parameter 变量被更改。
到目前为止它有效,因为警报框显示了我刚刚设置的值。
我现在想重新加载 PartialView 以便 @Model.Header 和 @Model.ImageUrl 更新,因为它们依赖于
我改变的参数。
我怎样才能做到这一点?
哦,顺便说一句,我对 JavaScript 完全陌生...
干杯,
西蒙
编辑:
好的,上面的两个答案确实有点帮助,但由于我是一个真正的 JavaScript 菜鸟,所以有点难...
我现在拥有的是:
@model MyViewModel
<div id="my-header">
<h4>@Model.Header</h4>
</div>
<table id="my-table" style="background-color: #f8f7f8; width:100%;">
<tr style="line-height:18px; background-color: #d6d6d6;">
<th style="cursor:pointer" id="value1">Value1</th>
<th style="cursor:pointer" id="value2">Value2</th>
<th style="cursor:pointer" id="value3">Value3</th>
<th style="cursor:pointer" id="value4">Value4</th>
<th style="cursor:pointer" id="value5">Value5</th>
<th style="cursor:pointer" id="value6">Value6</th>
<th style="cursor:pointer" id="value7">Value7</th>
</tr>
</table>
<div id="my-image">
<img src="@Model.ImageUrl" alt="Chart" />
</div>
<script type="text/javascript">
$('#my-table th').click(function() {
alert(this.id);
@Model.Parameter = this.id;
$('#my-header').html("<h4>@Model.Header</h4>");
});
</script>
到目前为止这很好,但我无法从我的模型类中操纵属性Parameter。所以调用@Model.Parameter = anyValue; 不起作用。
如何在 JavaScript 中做到这一点?
编辑 2:
好的,我终于按照 chaZm 建议的方式解决了它。 JSON 方式。
但首先我更改了我的 ViewModel,以便所有可能的 URL 和标头都存储在 Dictionary<string, string> 中。那么我的 PartialView 是:
@model MyViewModel
<div id="my-header">
<h4>@Model.Header</h4>
</div>
<table id="my-table" style="background-color: #f8f7f8; width:100%;">
<tr style="line-height:18px; background-color: #d6d6d6;">
<th style="cursor:pointer" id="value1">Value1</th>
<th style="cursor:pointer" id="value2">Value2</th>
<th style="cursor:pointer" id="value3">Value3</th>
<th style="cursor:pointer" id="value4">Value4</th>
<th style="cursor:pointer" id="value5">Value5</th>
<th style="cursor:pointer" id="value6">Value6</th>
<th style="cursor:pointer" id="value7">Value7</th>
</tr>
</table>
<div id="my-image">
<img src="@Model.ImageUrl" alt="Chart" />
</div>
<script type="text/javascript">
$('#my-table th').click(function() {
var urls = @Html.Raw(Json.Encode(@Model.Urls));
var url = urls[this.id];
$('#my-image').html("<img src='" + url + "' alt='Chart' />");
var headers = @Html.Raw(Json.Encode(@Model.Headers));
var header = headers[this.id];
$('#my-header').html(header);
});
</script>
现在一切都按预期进行......
感谢您的努力。
【问题讨论】:
标签: javascript asp.net-mvc asp.net-mvc-3