简体   繁体   English

通过 JavaScript 刷新 MVC 3 PartialView

[英]Refreshing MVC 3 PartialView via JavaScript

I'm having a ASP.NET MVC 3 view containing several PartialViews.我有一个包含几个 PartialViews 的 ASP.NET MVC 3 视图。 One of them gets an image from the internet which depends on parameters of its ViewModel.其中一个从 Internet 获取图像,该图像取决于其 ViewModel 的参数。 I'd like to change the parameters of the ViewModel and reload that PartialView (only) so that the image with the changed parameter is loaded.我想更改 ViewModel 的参数并重新加载 PartialView(仅),以便加载具有更改参数的图像。

Here's my PartialView so far:到目前为止,这是我的 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" />

So the thing is that I want to click one of the values (Value1-7) and the Parameter variable of my ViewModel is changed.所以问题是我想单击其中一个值(Value1-7)并且我的 ViewModel 的 Parameter 变量被更改。 So far it works, as the alert box shows the value I just set.到目前为止它有效,因为警报框显示了我刚刚设置的值。 I now want to reload the PartialView so that @Model.Header as well as @Model.ImageUrl are updated as they depend on the parameter that I changed.我现在想重新加载 PartialView,以便@Model.Header@Model.ImageUrl更新,因为它们取决于我更改的参数。 How can I do this?我怎样才能做到这一点? Oh by the way, I'm totally new to JavaScript...哦,顺便说一句,我对 JavaScript 完全陌生......

Cheers,干杯,
Simon西蒙

EDIT:编辑:
Ok, the two answers above did help a little, but as I am a real JavaScript noob it's kind of hard...好的,上面的两个答案确实有点帮助,但因为我是一个真正的 JavaScript 菜鸟,这有点难......
What I have now is:我现在拥有的是:

@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>

That is good so far, but I can't manage to manipulate the Property Parameter from my Model class.到目前为止这很好,但我无法从我的 Model class 操纵属性Parameter So the call @Model.Parameter = anyValue;所以调用@Model.Parameter = anyValue; doesn't work.不起作用。
How can I do this in JavaScript?如何在 JavaScript 中做到这一点?

EDIT 2: Ok, I finally solved it the way chaZm suggested.编辑 2:好的,我终于按照 chaZm 建议的方式解决了它。 The JSON way. JSON 方式。
But first of all I changed my ViewModel so that all possible URLs and headers are stored in a Dictionary<string, string> .但首先我更改了我的 ViewModel,以便所有可能的 URL 和标题都存储在Dictionary<string, string>中。 Then my PartialView is:然后我的 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>

Now everything works as it meant to be...现在一切都按预期进行......
Thanks for your efforts.感谢您的努力。

Well.出色地。 There are 2 ways to update MVC page.有两种方法可以更新 MVC 页面。

1) Full page refresh (you simply call controller action, it forms new view with updated parameters) 1)整页刷新(您只需调用 controller 动作,它 forms 新视图更新参数)

2) AJAX update. 2) AJAX 更新。 Javascript know nothing about what server thing you use. Javascript 对您使用的服务器一无所知。 MVC \ ASP>NET, anything. MVC \ ASP> NET,任何东西。 So what you need is call controller which would return you new partial view values.所以你需要调用 controller ,它会返回新的局部视图值。 Here you can return partial Value from server as在这里,您可以从服务器返回部分值

    public PartialViewResult Result()
    {
        return PartialView("Viewname",params);
    }

and then assign returned html to some container with jquery like然后将返回的 html 分配给一些带有 jquery 的容器

    $('div').load("/Result",....)

OR或者

You can return updated data via JSONResult like您可以通过 JSONResult 返回更新的数据,例如

    public JsonResult Result()
    {
        return Json(GetSomeValidJson());
    }

and then format the page you like in JS.然后在JS中格式化你喜欢的页面。 You getting Json, parsing it and setting manually all page data you like你得到 Json,解析它并手动设置你喜欢的所有页面数据

I didn't work with MVC 3 for too long (only 1 and 2) so there could be some updates in that area.我使用 MVC 3 的时间不长(只有 1 和 2),因此该领域可能会有一些更新。 But the basic are the same i think.但我认为基本是一样的。 Good luck!祝你好运!

By using a partial to are virtually merging it into the page.通过使用部分来将其虚拟地合并到页面中。 The web browser has no idea that that section of the page was from a partial (since MVC takes care of everything server side). web 浏览器不知道页面的该部分来自部分(因为 MVC 负责服务器端的所有内容)。 What I have done in the past when I need a certain section of the page to update is to create a separate action which returns just the value in the partial.当我需要更新页面的某个部分时,我过去所做的是创建一个单独的操作,它只返回部分中的值。 The partial doesn't need to include any <head> or <body> tags, just the content.部分不需要包含任何<head><body>标签,只需包含内容即可。 Then, on the client side, AJAX that data in.然后,在客户端,AJAX 输入数据。

For example, create a controller called MyAJAXPartials and an action for this section called MyPartial (I would change these names if I were you).例如,创建一个名为 MyAJAXPartials 的 controller 并为此部分创建一个名为 MyPartial 的操作(如果我是你,我会更改这些名称)。 Then, make that action render a regular view (not a partial), but use the same code as you posted above.然后,使该操作呈现常规视图(不是部分视图),但使用与您在上面发布的相同代码。 Using onclick="..." is not the preferred way to do action binding, if you are using jQuery (and you give the table an ID), you can do:使用onclick="..."不是执行操作绑定的首选方式,如果您使用的是 jQuery (并且您给表一个 ID),您可以执行以下操作:

$('#TABLE-ID th').each(function() {
    $(this).click(function() {
        changeParameter($(this).text()));
    });
});

Again, instead of using the partial, on the main page build a frame form the content with a <div> tag, give it an id like contentArea, and use the following JavaScript (again, I assume you have jQuery):同样,不是使用部分,而是在主页上构建一个带有<div>标签的内容框架,给它一个类似 contentArea 的 id,并使用以下 JavaScript(再次,我假设你有 jQuery):

$.get('/MyAJAXPartials/MyPartial', function(data) {
    $('#contentArea').html(data);
    // PUT THE CHANGE PARAMETER CODE I SUGGESTED ABOVE HERE
});

Then, each time that you need to change the data, just reload the content in that frame by requesting a new URL.然后,每次您需要更改数据时,只需通过请求新的 URL 重新加载该帧中的内容。 For example, instead of requesting /MyAJAXPartials/MyPartial , it could be /MyAJAXPartials/MyPartial/Value1 .例如,不是请求/MyAJAXPartials/MyPartial ,而是/MyAJAXPartials/MyPartial/Value1 All of the JavaScript should be outside of that frame.所有 JavaScript 都应该在该框架之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM