我有一個包含多個PartialViews的ASP.NET MVC 3視圖。其中一個從互聯網上獲取圖像,該圖像取決於其ViewModel的參數。我想更改ViewModel的參數並重新加載該PartialView(僅),以便加載具有更改的參數的圖像。通過JavaScript刷新MVC 3部分視圖
這裏是我的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)和我的視圖模型的參數變量發生變化的一個。 到目前爲止,它的工作,因爲警報框顯示我剛剛設置的值。 我現在想重新加載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>
現在一切正常,因爲它意味着是...
感謝你的努力。
請看看我的編輯。也許你可以幫助我... –