简体   繁体   English

Javascript有条件地构建mvc actionlink

[英]Javascript to build mvc actionlink conditionally

This is in a VB.NET MVC 3 Razor view and fires when a JsonResult of success is returned. 这是在VB.NET MVC 3 Razor视图中,并在返回成功的JsonResult时触发。 The problem is that I would like to conditionally build an actionlink if data.Object.Status == 'Completed'; 问题是如果data.Object.Status == 'Completed'; ,我想有条件地构建一个actionlink data.Object.Status == 'Completed';

I have looked around and nothing seems to be fitting at all to solve this. 我环顾四周,似乎没有任何东西可以解决这个问题。 This is what the actionlink should look like in razor: 这就是actionlink在剃须刀中的样子:

@Html.ActionLink("Completed(Print Cert)", "Ind_Cert", "Printing", New With {.firstName = currentItem.firstName, .lastname = currentItem.lastName, .classRef = currentItem.course_ref, .cNumber = currentItem.conf_number}, Nothing)

And this is the javascript function that will do it. 这是将执行此操作的javascript函数。 Currently it just Places the contents of data.Object.Status . 目前它只是data.Object.Status的内容。 Which should only show like that when data.Object.Status != 'Completed' ; 哪个应该只显示data.Object.Status != 'Completed' ;

  function updateSuccess(data) {
    if (data.Success == true) {
        //we update the table's info
        var parent = linkObj.closest("tr");
        parent.find(".CompletedClass").html(data.Object.Status);
        //now we can close the dialog
        $('#updateDialog').dialog('close');
        //twitter type notification
        $('#commonMessage').html("Update Complete");
        $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

Below is what I am thinking will work I am still trying to figure it out but this is a rough markup of it. 以下是我的想法将起作用我仍在试图解决它,但这是一个粗略的标记。

   function updateSuccess(data) {
    if (data.Success == true) {
        //we update the table's info
        var parent = linkObj.closest("tr");
        var d = parent.find(".CompletedClass");
        if (data.Object.Status == 'Completed') {
            d.html = @Html.ActionLink("Completed(Print Cert)", "Ind_Cert", "Printing", New With {.firstName = Model(0).firstName, .lastname = Model(0).lastName, .classRef = Model(0).Completed_Class, .cNumber = Model(0).conf_number}, Nothing) 
            }


        //now we can close the dialog
        $('#updateDialog').dialog('close');
        //twitter type notification
        $('#commonMessage').html("Update Complete");
        $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

What you suggest right now should work, there is a typo on your code assigning the HTML, you probably know it but the way you add HTML to an element is (You have a = sign which is not valid) : 你现在建议的应该是什么工作,你的代码分配HTML有一个错字,你可能知道它,但你添加HTML到元素的方式是(你有一个无效的符号):

d.html (@Html.ActionLink("Completed(Print Cert)", "Ind_Cert", ....) 

Assuming is an element that accepts html, otherwise, use the "text" function. 假设是一个接受html的元素,否则,使用“text”函数。

In any case, my recommendation would be to generate the HTML before hand but have it hidden on the page (unless you have thousands of them, of there are security concerns). 在任何情况下,我的建议是先手动生成HTML,但将其隐藏在页面上(除非你有数千个,有安全问题)。 Then you just have your Javascript simply show the element: 然后你只需要你的Javascript只显示元素:

 var parent = linkObj.closest("tr");
 var linkElement = parent.find(".mylinkelement-class");
    if (data.Object.Status == 'Completed') {
        linkElement.show();
        }

This should give you the benefit of allowing better separation between your Javascript and your MVC code. 这应该可以让您更好地分离Javascript和MVC代码。

HTH, -Covo HTH,-Covo

You should update your code to something like this 您应该将代码更新为这样的代码

d.html('@Html.ActionLink("Completed(Print Cert)", "Ind_Cert", "Printing", New With {.firstName = Model(0).firstName, .lastname = Model(0).lastName, .classRef = Model(0).Completed_Class, .cNumber = Model(0).conf_number}, Nothing)');

To assign html with jQuery you should use jQuery.html() . 要使用jQuery分配html,您应该使用jQuery.html()

The Razor @Html.ActionLink will print HTML to the page, to not break your Javascript, put those inside quotes d.html('@Html...') Razor @Html.ActionLink将打印HTML到页面,不打破你的Javascript,把它们放在引号d.html('@Html...')

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

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