JavaScript变量进行,我需要在Html.ActionLink使用一个ID [英] JavaScript variable carry an ID that I need to use in a Html.ActionLink

查看:2425
本文介绍了JavaScript变量进行,我需要在Html.ActionLink使用一个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的jQuery code包含我需要在我的 Html.ActionLink ,但它不工作使用一个ID的JavaScript变量:

I have a JavaScript variable in my jQuery code that contains an ID that I need to use in my Html.ActionLink but it's not working:

@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI", new {id = goalcard.Id},null))

我得到:无法解析符号goalcard,原因是goalcard是一个JavaScript变量

I get: 'cannot resolve symbol "goalcard"', and the reason is that goalcard is a JavaScript variable.

这是什么样子:

$.post('@Url.Action("Search", "SearchNKI")', data, function (result) {
    $("#GoalcardSearchResult tbody").empty();
    result.forEach(function(goalcard) {
        $("#GoalcardSearchResult tbody").append(
            $('<tr/>', {
            // steg Create a row for each result 
                html: "<td>" + goalcard.Name +
                "</td><td>" + goalcard.Customer +
                "</td><td>" + goalcard.PlannedDate +
                "</td><td>" + goalcard.CompletedDate +
                "</td><td>" + '@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI", new {id = goalcard.Id},null))' + "</td>"
        }));
    });
});

我一直在测试的同时,我几乎找到了解决方案,它看起来是这样的:

I have been testing for while now and I almost found a solution and it looks like this:

@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI",null, new {id = "mylink"}))

然后我做了一个新功能:

then I made a new function:

$('#mylink').click(function (goalcard) {
    var id = goalcard.Id;
    this.href = this.href + '/' + id;
});

这应该工作,但发生的事情是,我要使用的forEach 循环这里面点击功能,能够达到goalcard变量。如果我把它放在的forEach ,点击这个函数会被执行多次内部取决于有多少goalcard有。

This should work, but what happens is that I have to use this click function inside the forEach loop to be able to reach to goalcard variable. and If I put it inside the forEach, this Click function will get executed many times depending on how many goalcard there are.

因此​​,结果将是 / AnswerNKI / AnswerForm / 1/2 如果有两个goalcards。
或者 / AnswerNKI / AnswerForm / 1/2/3/4/5 如果有五个goalcards。

So the result would be /AnswerNKI/AnswerForm/1/2 if there are two goalcards. or maybe /AnswerNKI/AnswerForm/1/2/3/4/5 if there are five goalcards.

但它应该是 / AnswerNKI / AnswerForm / 1

它基本上增加了。

另一个问题是,所有其他行有 / AnswerNKI / AnswerForm / 所以只有第一行基本上得到一个ID。

Another problem is that all other rows have /AnswerNKI/AnswerForm/ so only the first row basically gets an id.

我不知道如何找到一个解决方案来解决这个问题。

I have no idea how to find a solution to fix that.

任何形式的帮助是非常AP preciated。

Any kind of help is very appreciated.

在此先感谢

推荐答案

这是不是您遇到的具体问题的解决方案。但它看起来像你使用jQuery来更新页面的一部分。

This isn't a solution to the specific problem you're having. But it looks like you're using jquery to update part of your page.

如果是这样,你有使用的返回HTML由PartialView产生回调,而只是做了JavaScript回调中的替换考虑?这是我用了很多的模式。它可以让你使用MVC发动机,零部件和设计工具。

If so, have you considered using a callback which returns html generated by a PartialView, and just doing a replace within the javascript callback? That's a pattern I use a lot. It allows you to use the MVC engine, components and design tools.

下面是我做的东西的例子:

Here's an example of something I do:

$("form.unscoped_filter").live("submit", function () {
    $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            error: function (a, b) {
                debugger;
            },
            success: function (result) {
                $("div#targetID").html(result);
                bindExtenders();
            }
        });
        return false;
    });

这个特殊的例子截获特定类的形式回发,并将其传递到哪里它是由我的MVC应用程序处理的网站。目标方法和控制器设置,按通常的MVC设计方法,在BeginForm块。目标方法处理数据,并返回一个转换的数据转换成HTML,这反过来又被送到成功回调jQuery的块PartialView。目前它被用来替换目标专区内的HTML。虽然在戏更多的零部件(如JavaScript中,MVC法,PartialView),分离的东西这样可以让每个部分发挥其独特的优势:jQuery是在操纵DOM精彩,MVC方法是在操纵很大/处理对HTML请求,PartialViews是布局和生成HTML一个伟大的工具。

This particular example intercepts a postback from a particular class of forms and passes it on to the website where it's processed by my MVC app. The target method and controller are set, per usual MVC design approaches, in a BeginForm block. The target method processes data and returns a PartialView which translates that data into html, which in turn is sent to the success callback in the jquery block. There it's used to replace the html within the target div. While there are more parts in play (e.g., the javascript, the MVC method, the PartialView), separating things this way lets each part play to its unique strengths: jquery is wonderful at manipulating the DOM, MVC methods are great at manipulating/processing requests for html, and PartialViews are a great tool for laying out and generating html.

这是一个pretty灵活而强大的方式,一旦你得到了它的窍门。特别是,你可以通过使用HTML5技术参数传递到从ActionLink的jquery的块。例如,您可以在数据someParamName = ...添加到ActionLink的调用的HTML属性,然后提取的JavaScript块做类似的内部参数值:

This is a pretty flexible and powerful approach once you get the hang of it. In particular, you can pass parameters into the jquery block from the ActionLink by using html5 techniques. For example, you can add a "data-someParamName=..." to the html attributes of the ActionLink call, and then extract the parameter value within the javascript block doing something like:

var pagerCode = $(this).attr("data-someParamName");

这,其实是我如何控制哪些特定的div在成功回调函数被更新。

That, in fact, is how I control which particular div gets updated in the success callback function.

此外,这并不意味着作为解决什么,我认为你试图做一个不同的方式回答您的具体问题这么多。祝你好运!

Again, this doesn't answer your specific question so much as suggest a different way of tackling what I think you're trying to do. Good luck!

这篇关于JavaScript变量进行,我需要在Html.ActionLink使用一个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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