简体   繁体   English

JavaScript变量带有我需要在Html.ActionLink中使用的ID

[英]JavaScript variable carry an ID that I need to use in a Html.ActionLink

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: 我的jQuery代码中有一个JavaScript变量,其中包含我需要在Html.ActionLink使用的ID,但它不起作用:

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

I get: 'cannot resolve symbol "goalcard"', and the reason is that goalcard is a JavaScript variable. 我得到:'无法解析符号“goalcard”',原因是goalcard是一个JavaScript变量。

This is what it looks like: 这就是它的样子:

$.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;
});

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. 这应该有效,但是我必须在forEach循环中使用这个click函数才能达到goalcard变量。 and If I put it inside the forEach , this Click function will get executed many times depending on how many goalcard there are. 如果我把它放在forEach ,这个Click函数将被执行多次,具体取决于有多少个目标卡。

So the result would be /AnswerNKI/AnswerForm/1/2 if there are two goalcards. 因此,如果有两个守门员,结果将是/AnswerNKI/AnswerForm/1/2 1/2。 or maybe /AnswerNKI/AnswerForm/1/2/3/4/5 if there are five goalcards. 或者可能是/AnswerNKI/AnswerForm/1/2/3/4/5如果有五个守门员。

But it should be /AnswerNKI/AnswerForm/1 但它应该是/AnswerNKI/AnswerForm/1

it basically adds up. 它基本上加起来了。

Another problem is that all other rows have /AnswerNKI/AnswerForm/ so only the first row basically gets an id. 另一个问题是所有其他行都有/AnswerNKI/AnswerForm/所以只有第一行基本上得到一个id。

I have no idea how to find a solution to fix that. 我不知道如何找到解决方案来解决这个问题。

Any kind of help is very appreciated. 非常感谢任何形式的帮助。

Thanks in advance 提前致谢

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. 但看起来您正在使用jquery来更新页面的一部分。

If so, have you considered using a callback which returns html generated by a PartialView, and just doing a replace within the javascript callback? 如果是这样,你是否考虑过使用一个回调来返回由PartialView生成的html,并且只是在javascript回调中进行替换? That's a pattern I use a lot. 这是我经常使用的模式。 It allows you to use the MVC engine, components and design tools. 它允许您使用MVC引擎,组件和设计工具。

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;
    });

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. 此特定示例拦截来自特定类表单的回发,并将其传递到由我的MVC应用程序处理的网站。 The target method and controller are set, per usual MVC design approaches, in a BeginForm block. 根据通常的MVC设计方法,在BeginForm块中设置目标方法和控制器。 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. 目标方法处理数据并返回一个PartialView,它将该数据转换为html,然后将其发送到jquery块中的成功回调。 There it's used to replace the html within the target div. 它用于替换目标div中的html。 While there are more parts in play (eg, 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. 虽然有更多的部分在运行(例如,javascript,MVC方法,PartialView),但是通过这种方式分离事物可以让每个部分发挥其独特的优势:jquery非常适合操作DOM,MVC方法非常适合操作/处理对html的请求,PartialViews是一个用于布局和生成html的好工具。

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. 特别是,您可以使用html5技术将参数从ActionLink传递到jquery块。 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: 例如,您可以将“data-someParamName = ...”添加到ActionLink调用的html属性中,然后在javascript块中提取参数值,执行以下操作:

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

That, in fact, is how I control which particular div gets updated in the success callback function. 实际上,这就是我如何控制在成功回调函数中更新哪个特定div。

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! 祝好运!

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

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