javascript 如何使用在 jscript 中声明的参数调用 Html.RenderAction()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10897433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:24:42  来源:igfitidea点击:

How to call Html.RenderAction() with parameters declarated in jscript?

c#javascriptjqueryasp.net-mvc

提问by Dmytro

I have a javascript in *.cshtml file

我在 *.cshtml 文件中有一个 javascript

$(function () {

sliderDiv.slider({
  range: true,
  min: minVal,
  max: maxVal,
  values: [minVal, maxVal]
});

sliderDiv.bind("slidechange", function (event, ui) {

  var d = "min=" + ui.values[0] + "&max=" + ui.values[1];
  $.ajax({
    type: "POST",
    url: '@Url.Action("Update", "Default")',
    data: d,
    success: function (result, ajaxObj) {
        alert('ok');
        alert(result.min + " - " + result.max);
        $("#ajaxresult").html('@{Html.RenderAction("Update", "Default");}');
    },
   error: function (ajaxObj, message, exceptionObj) { alert('no'); }
});
});
}

And Controller:

和控制器:

public ActionResult Update(int? min, int? max)
        {
            if(min == null) min = 1;
            if(max == null) max = 1;
            var s = new SliderModel()
                                {
                                    min = (int)min * 1000,
                                    max = (int)max * 1000
                                };

            return new JsonResult
                       {
                           Data = s,
                           ContentEncoding = Encoding.UTF8,
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                           ContentType = "json"
                       };

        }

I want to use this line

我想用这条线

$("#ajaxresult").html('@{Html.RenderAction("Update", "Default");}');

to send ui.values[0]and ui.values[1]as minand maxparameters to Html.RenderAction("Update", "Default")

发送ui.values[0]ui.values[1]作为minmax参数Html.RenderAction("Update", "Default")

Something like $("#ajaxresult").html('@{Html.RenderAction("Update", "Default", new {min = ui.values[0], max = ui.values[1]});}');

就像是 $("#ajaxresult").html('@{Html.RenderAction("Update", "Default", new {min = ui.values[0], max = ui.values[1]});}');

How can I do something like that???

我怎么能做这样的事情???

回答by gdoron is supporting Monica

var url = '@Url.Action("Update", "Default")';
url += '/?min=' + ui.values[0] + '&max=' + ui.values[1];
$("#ajaxresult").load(url);

loaddocs:

load文档

Description: Load data from the server and place the returned HTML into the matched element.

描述:从服务器加载数据并将返回的 HTML 放入匹配的元素中。