如何调用URL行动MVC与javascript函数? [英] How to call URL action in MVC with javascript function?

查看:77
本文介绍了如何调用URL行动MVC与javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试呈现一个MVC项目,JavaScript网址行动。我抓住我的网页上调用该函数,但我不确定如何调用该特定URL中的事件。

谁能帮助我吗? :)

 函数onDropDownChange(五){
    VAR URL ='/首页/索引/'+ e.value;
    //现在怎么办...?
}

----------- ------------编辑

Here's我的控制器操作:

 公众的ActionResult指数(INT?ID)
    {
        VAR tmpToday = DateTime.Now;
        变种今天=新日期时间(tmpToday.Year,tmpToday.Month,tmpToday.Day,0,0,0);        如果(ID!= NULL)
        {
            VAR NUM = id.GetValueOrDefault();
            VAR rentableUnits =新的List<单元>();
            _unitLogic.GetAllRentableUnitsByArea(NUM,rentableUnits);            VAR allAvailabilities =新ShowAvailability [rentableUnits.Count]            的for(int i = 0; I< rentableUnits.Count;我++)
            {
                VAR STIME = GetFirstDayOfMonth(今天);
                VAR ETIME = GetLastDayOfMonth(今天);
                allAvailabilities [I] =新ShowAvailability(STIME,eTime.AddHours(23.0).AddMinutes(59.0),rentableUnits);
                今日= today.AddMonths(1);
            }            VAR showAvailability =新的List< ShowAvailability>(allAvailabilities);            返回查看(新HomeFormViewModel(showAvailability));
        }
        其他
        {
            VAR allAvailabilities =新ShowAvailability [12];            的for(int i = 0; I< 12;我++)
            {
                VAR STIME = GetFirstDayOfMonth(今天);
                VAR ETIME = GetLastDayOfMonth(今天);
                allAvailabilities [I] =新ShowAvailability(STIME,eTime.AddHours(23.0).AddMinutes(59.0));
                今日= today.AddMonths(1);
            }            VAR showAvailability =新的List< ShowAvailability>(allAvailabilities);            返回查看(新HomeFormViewModel(showAvailability));
        }
    }

#使用Telerik的扩展我的DropDownList时触发JavaScript函数

I'm,其实这是一个剃刀查看:

  @(Html.Telerik()。DropDownList的()
     。名称(DropDownList的)
     .Items(面积=>
         {
             。area.Add()文本(OLLsvæði)值(0)选中状态(true);
             的foreach(在Model.Areas单位一)
                {
                     。area.Add()文本(a.Name).value的(a.UnitID.ToString());
                }
         })
     .HtmlAttributes(新{风格=WIDTH:130px;})
     .ClientEvents(CLEV = GT; clev.OnChange(onDropDownChange))
     )

Here's脚本:

 函数onDropDownChange(五){
    VAR URL ='/首页/索引/'+ e.value;
    $阿贾克斯({
            键入:GET,
            网址:网址,
            数据:{},
            数据类型:HTML
        });
}


解决方案

在你的 onDropDownChange 的处理程序,只是做一个jQuery的AJAX调用,传入你需要传递的任何数据到您的网址。您可以处理好与成功错误选项成功和失败的电话。在成功选项,使用包含在数据数据参数做任何渲染,你需要做的。请记住这些都是默认异步!

 函数onDropDownChange(五){
    VAR URL ='/首页/索引/'+ e.value;
    $阿贾克斯({
      网址:网址,
      数据:{} //参数去这里对象文本形式
      输入:GET,
      数据类型:JSON,
      成功:功能(数据){警报('数据到这儿'); },
      错误:函数(){警报('坏事发生'); }
    });
}

jQuery的AJAX文档这里

I´m trying to render url action with javascript in an MVC project. I capture an event on my page which calls this function but I´m not sure how to call this certain URL.

Can anyone help me please? :)

function onDropDownChange(e) {
    var url = '/Home/Index/' + e.value;
    //What now...?
}

-----------Edited-----------------------

Here´s my controller action:

    public ActionResult Index(int? id)
    {
        var tmpToday = DateTime.Now;
        var today = new DateTime(tmpToday.Year, tmpToday.Month, tmpToday.Day, 0, 0, 0);

        if (id != null)
        {
            var num = id.GetValueOrDefault();
            var rentableUnits = new List<Unit>();
            _unitLogic.GetAllRentableUnitsByArea(num, rentableUnits);

            var allAvailabilities = new ShowAvailability[rentableUnits.Count];

            for (int i = 0; i < rentableUnits.Count; i++)
            {
                var sTime = GetFirstDayOfMonth(today);
                var eTime = GetLastDayOfMonth(today);
                allAvailabilities[i] = new ShowAvailability(sTime, eTime.AddHours(23.0).AddMinutes(59.0), rentableUnits);
                today = today.AddMonths(1);
            }

            var showAvailability = new List<ShowAvailability>(allAvailabilities);

            return View(new HomeFormViewModel(showAvailability));
        }
        else
        {
            var allAvailabilities = new ShowAvailability[12];

            for (int i = 0; i < 12; i++)
            {
                var sTime = GetFirstDayOfMonth(today);
                var eTime = GetLastDayOfMonth(today);
                allAvailabilities[i] = new ShowAvailability(sTime, eTime.AddHours(23.0).AddMinutes(59.0));
                today = today.AddMonths(1);
            }

            var showAvailability = new List<ShowAvailability>(allAvailabilities);

            return View(new HomeFormViewModel(showAvailability));
        }
    }

#

I´m using Telerik extension for my DropDownList which fires the javascript function, this is in fact a Razor View:

 @(Html.Telerik().DropDownList()
     .Name("DropDownList")
     .Items(area =>
         {
             area.Add().Text("Öll svæði").Value("0").Selected(true);
             foreach (Unit a in Model.Areas)
                {
                     area.Add().Text(a.Name).Value(a.UnitID.ToString());
                }
         })
     .HtmlAttributes(new { style = "width: 130px;" })
     .ClientEvents(clev => clev.OnChange("onDropDownChange"))
     )

Here´s the script:

function onDropDownChange(e) {
    var url = '/Home/Index/' + e.value;
    $.ajax({
            type: "GET",
            url: url,
            data: {},
            dataType: "html"
        });
}

解决方案

Within your onDropDownChange handler, just make a jQuery AJAX call, passing in any data you need to pass up to your URL. You can handle successful and failure calls with the success and error options. In the success option, use the data contained in the data argument to do whatever rendering you need to do. Remember these are asynchronous by default!

function onDropDownChange(e) {
    var url = '/Home/Index/' + e.value;
    $.ajax({
      url: url,
      data: {}, //parameters go here in object literal form
      type: 'GET',
      datatype: 'json',
      success: function(data) { alert('got here with data'); },
      error: function() { alert('something bad happened'); }
    });
}

jQuery's AJAX documentation is here.

这篇关于如何调用URL行动MVC与javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆