Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在MVC razor视图中将值从jQuery脚本传递到ActionLink_Javascript_Jquery_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

Javascript 如何在MVC razor视图中将值从jQuery脚本传递到ActionLink

Javascript 如何在MVC razor视图中将值从jQuery脚本传递到ActionLink,javascript,jquery,asp.net-mvc,asp.net-mvc-4,razor,Javascript,Jquery,Asp.net Mvc,Asp.net Mvc 4,Razor,大家好,我希望你们能提出一个简短的问题 我想将jQuery脚本中的一个值传递到HTML.ActionLink中,以在控制器中详细说明操作 它就像我在dropdownlist中选择项目一样,在jQuery中,我得到该项目的id,然后我想把该id传递到ActionLink,我如何实现它,或者我希望在代码中更改什么 <script> $(function() { $('#reductions').change(function () { var Id =

大家好,我希望你们能提出一个简短的问题

我想将jQuery脚本中的一个值传递到HTML.ActionLink中,以在控制器中详细说明操作

它就像我在dropdownlist中选择项目一样,在jQuery中,我得到该项目的id,然后我想把该id传递到ActionLink,我如何实现它,或者我希望在代码中更改什么

<script>
$(function() {
       $('#reductions').change(function () {
           var Id = $('#reductions').val();
           });
       });
</script>

        <div class="form-horizontal">
            <div class="form-group">
                <div class="col-md-10">
 @Html.DropDownList("reductions", new SelectList(dbContext.Reductions,"ReductionId", "Name")  
                </div>
            </div>
        </div>
@Html.ActionLink("Details", "Details", "Reductions", new {id = ????WHAT PUT HERE?????}, new {})
请告诉我我应该在脚本区域或行动链接中更改什么,我可以使我工作。
我已经研究了很多示例,但似乎没有人在为我工作,而不是使用ActionLink方法创建一个普通的HTML链接并用jquery覆盖href属性

<script>
   $(function() {
    var link = '@Url.Action("Details", "Details", "Reductions", new { id = "_myid_" })';
    $('#reductions').change(function () {
       var id = $('#reductions').val();
       $('#myLink').attr('href', link.replace('_myid_', id);
       });
   });
</script>

... Your other html

<a id="myLink" href="">Details</a>

不要使用普通HTML表单,请使用以下内容:

@using(HTML.BeginForm("Details","Reductions"))
{
  //Your current HTML
}
您还应该使用@HTML.DropDownListFor帮助程序,如下所示:

@Html.DropDownListFor("reductions", new SelectList(DBContext.Reductions, "ReductionId", "Name"), new { @class = "anyCSSClass" })
button type="submit" class="CSSClass" id="generatedContent"> Generate </button>
将当前@HTML.DropDownList帮助程序替换为此帮助程序

要完成此操作,必须向服务器提交表单数据,如下所示:

@Html.DropDownListFor("reductions", new SelectList(DBContext.Reductions, "ReductionId", "Name"), new { @class = "anyCSSClass" })
button type="submit" class="CSSClass" id="generatedContent"> Generate </button>
以下是最终代码:

@using(HTML.BeginForm("Details","Reductions"))
{
  @Html.DropDownListFor("reductions", new SelectList(DBContext.Reductions,  "ReductionId", "Name"), new { @class = "anyCSSClass" })

  button type="submit" class="CSSClass" id="generatedContent"> Generate </button>
}
您可以使用JQuery/AJAX来实现这一点,但这比它的价值要麻烦得多,因为它违背了MVC的基本概念,并将服务器端逻辑与客户端逻辑分开


另外,如果我混淆了控制器/操作方法的名称,请检查这两个助手的重载方法

请发布呈现的操作链接html。我遵循了您的答案,对我来说似乎是最好的,因为我已经将jQuery用于其他目的,并在单击按钮后使用KyleT answer interrupt调用控制器操作。谢谢你的帮助。