1

我正在使用带有参数的 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,该 Model.VIN 正在设置为 Get 操作....但我在调用试图使用 Model.WorkOrderID 的 Manage WorkOrder Action 链接时得到零设置方式与我在第一个示例中设置模型值的方式相同......

@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>

但我不知道如何将新操作链接的这个值返回到 html....我知道这是基本的 javascript....但是我使用什么标签???我的动作链接在

  • 标签....我不知道该怎么做:

    <li> create the action link here </li>  
    
  • 4

    1 回答 1

    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然后将 url 中的值替换为WorkOrderId动态获取的新值。

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

    于 2013-10-04T19:53:02.357 回答