html.actionlinks使用隐藏值作为参数

时间:2013-10-04 12:41:47

标签: javascript asp.net-mvc razor

我正在使用带参数的html.actionlinks ....我有一个简单的例子,其中值来自模型工作....

然而,对于一个actionLink,我从devexpress网格动态获取值。

对于另一种形式,我让它工作,我动态获取值并将其传递给HTML.BeginForm

鉴于这两个例子,我不明白我在下面做错了什么......

这个工作:

@using EtracsWeb.Areas.IO.Models;
@model VoyageInputModel


<script type="text/javascript">
    //var voyage_ids;
    function OnSelectionChanged(s, e) {
        s.GetSelectedFieldValues("VoyageID", GetSelectedFieldValuesCallback);
    }
    function GetSelectedFieldValuesCallback(values) {
        //  voyage_ids = values;
        //alert(voyage_ids);
        document.getElementById('VoyageIDS').value = values;
    }
</script>




@using (Html.BeginForm("Index", "Voyage",
              new { VoyageID = 0, Model.VoyageIDS, Model.ShowReceivedVoyages, Model.ETADate, Model.VoyageReceivedDate }))
 {

  <div class="divTable"  >
       <div class="divTableRow">
            <div class="divTableCell"> <input type="submit"  value="Receive"   name="ReceiveVoyage"    />  </div>
            <div class="divTableCell">  @Html.TextBoxFor(model => model.VoyageReceivedDate,  new{ @class = "date" }) </div>

       </div>

    </div>

    @* Gets the selected values from the grid and stores it in HTML so that beginForm can access*@
    @Html.Hidden("VoyageIDS", Model.VoyageIDS)


 }

注意......我没有显示devExpress Grid,但它正在调用OnSelectionChanged javascript ...

这是我的第二个代码示例,现在使用ActionLinks。在这个例子中,我让它用于传递Model.VIN,它正在Get action ....上设置但是我在调​​用Manage WorkOrder Action链接时得到零,该链接试图使用Model.WorkOrderID设置的方式与我在第一个示例中设置Model值的方式相同....

@using EtracsWeb.Areas.Vehicle.Models
@model VehicleVinInquiryData


@{
    ViewBag.Title = "VinInquiry";
    Layout = "~/Views/Shared/_LayoutMenu.cshtml";
}

@* MUST go here and NOT at end or code won't work *@
<script type="text/javascript">
    //var wo_id;
    function OnSelectionChanged(s, e) {
        s.GetSelectedFieldValues("WorkOrderID", GetSelectedFieldValuesCallback);
    }
    function GetSelectedFieldValuesCallback(values) {
       // alert(values);
      //  wo_id = values;
        document.getElementById('xWorkOrderID').value = values;
        // document.getElementById('xxID').value = values;
        alert(document.getElementById('xWorkOrderID').value);
    }
</script>



@using (Html.BeginForm("VinInquiry", "Info"))
{    
    <div class="divTable"  >
        <div class="divTableRow">
            <div class="divTableCell">  <label>VIN</label> </div>        
            <div class="divTableCell">  @Html.TextBoxFor(m => m.VIN)  </div>
             <div class="divTableCell"> <input type="submit"  value="Display"  />  </div>
            <div class="divTableCell">  <input type="submit"  value="Clear"  />  </div>

            <div class="divTableCell">             
                <ul id="nav">
                    <li>
                        <a href="#">Actions</a>
                        <ul>
                            <li>@Html.ActionLink("Send an Email"                         , "Index"             , "Email"              , new { Area = "IO"        } , null)</li>                      
                            <li>@Html.ActionLink("Attach a File"                         , "AttachFile"        , "Management"         , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Add a Comment"                         , "Create"            , "Comment"            , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Add Allocation"                        , "Create"            , "Allocation"         , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Change Allocation"                     , "Edit"              , "Allocation"         , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Delete/Cancel Allocation"              , "Delete"            , "Allocation"         , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("View Dealers"                          , "List"              , "Dealer"             , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Change HotRush Status"                 , "EditHotRushStatus" , "Management"         , new { Area = "Vehicle"   } , null)</li>
                            <li>@Html.ActionLink("Relocate Vehicle (Pass VehicleID)"     , "RelocateVehicle"   , "Location"           , new { Area = "Vehicle" ,VIN=Model.VIN } , null)</li>
                            <li>@Html.ActionLink("View Service Requests (Pass VehicleID" , "Index"             , "ServiceRequest"     , new { Area = "Vehicle" ,VIN=Model.VIN } , null)</li>
                            <li>@Html.ActionLink("Create a Voyage-Ship Entry"            , "AddVoyageShipEntry", "Voyage"             , new { Area = "IO"        } , null)</li>
                            <li>@Html.ActionLink("Process Manager"                       , "ProcessManager"    , "Management"         , new { Area = "IO"        } , null)</li>
                            <li>@Html.ActionLink("Manage WorkOrder (pass WorkOrderID)"   , "Index"             , "WorkOrderManagement", new { Area = "WorkOrder" , WorkOrderID=Model.WorkOrderID } , null)</li>
                        </ul>
                    </li>
                </ul>
           </div>
        </div>      
    </div>

   @* <input type="text" value="xxID" name="xxID" />  *@
    @Html.Hidden("xWorkOrderID" , Model.WorkOrderID)



}

警报正在运行,所以我知道值正在进入隐藏变量......

作为替代方案,我可以使用以下内容获取在javascript中生成的整个actionLink:

<script type="text/javascript">
    var wo_id;

    function OnSelectionChanged(s, e) {
        s.GetSelectedFieldValues("WorkOrderID", GetSelectedFieldValuesCallback);
    }
    function GetSelectedFieldValuesCallback(values) {
        // alert(values);
        wo_id = values;
        document.getElementById('sWorkOrderID').value = values;  
        alert(document.getElementById('sWorkOrderID').value);

        var atext = '@Html.ActionLink("Manage WorkOrder", "Index", "WorkOrderManagement", new { Area = "WorkOrder" , WorkOrderID="xxx" } , null)'.replace('xxx', wo_id);
        alert(atext);
        $("#myTag") = atext;
    }

</script>

但是我无法弄清楚如何将这个新的actionlink的值恢复到html ....我知道这是基本的javascript ....但是我使用什么标签?我的actionlink位于

  • 标签内....我无法弄清楚如何执行此操作:

    <li> create the action link here </li>  
    
  • 1 个答案:

    答案 0 :(得分:1)

    您不能将服务器端帮助器Html.ActionLink()与隐藏值一起使用。您正在尝试在首次呈现页面后更新客户端上的链接标记。

    你最后一次尝试几乎就在那里。试试这个:

    <ul>
        <li><!-- initially this is just a placeholder link -->
        @Html.ActionLink("Manage WorkOrder", "Index", "WorkOrderManagement",
            routeValues: new { Area = "WorkOrder" , WorkOrderID=Model.WorkOrderID },
            htmlAttributes: new { id="workorderlink" })
        </li>
    </ul>
    
    function GetSelectedFieldValuesCallback(values) {
        var wo_id = values;
        $("#xWorkOrderID").val(values);  // update hidden input
        var link = $("#workorderlink");
        var oldhref = link.attr("href");
        var newhref = oldhref.substring(0, oldhref.lastIndexOf('/')) + "/" + wo_id;
        link.attr("href", newhref);  // update the link
    }
    

    回调抓取原始href,然后使用新动态获取的值替换网址中的WorkOrderId值。

    注意:看来你正在使用jquery,所以我保持一致并使用jquery样式而不是getElementById