简体   繁体   English

未传递MVC Html.ActionLink参数值

[英]MVC Html.ActionLink parameter values not being passed

I'm testing MVC for a demo and I managed to scrap together some pieces but now I am having trouble with an Html.ActionLink. 我正在测试MVC的演示,并设法拼凑了一些片段,但是现在我遇到了Html.ActionLink的问题。 The intent is to present the user with a series of dropdownlists that they must select before the ActionLink is shown. 目的是向用户显示一系列在显示ActionLink之前必须选择的下拉列表。 To do that I've copied some JQuery to hide/show my dropdownlists (as selections are made) and the ActionLink. 为此,我复制了一些JQuery以隐藏/显示我的下拉列表(根据选择)和ActionLink。 I added an alert to my JQuery to check my values and via the alert it all looks good. 我在我的JQuery中添加了一个警报来检查我的值,并通过警报看起来一切都很好。 But if I debug the controller the parm values are defaulted to 0. I'm not sure what code to include but I will try to include the relevant parts. 但是,如果我调试控制器,则parm值默认为0。我不确定要包括什么代码,但是我将尝试包括相关部分。 I think it's something basic. 我认为这是基本的东西。

Here are the dropdown lists and ActionLink. 这是下拉列表和ActionLink。

@Html.DropDownListFor(m => m.selected_env_ID, new SelectList(Model.Environments, "env_ID", "env_DESC"), "*Select an environment")
@Html.DropDownListFor(m => m.selected_app_ID, new SelectList(Model.Applications, "app_ID", "app_DESC"), "*Select an application",new { @hidden = "hidden" })
@Html.DropDownListFor(m => m.selected_job_ID, Enumerable.Empty<SelectListItem>(), "*Select a job", new { @hidden = "hidden" })
@Html.ActionLink("Submit", "Submit", new { id =  Model.selected_job_ID, envid = Model.selected_env_ID }, new {id = "lnkSubmit" }) 

Here is the convoluted JQuery to hide/show and fill the cascading dropdowns. 这是令人费解的JQuery,用于隐藏/显示和填充级联下拉列表。

<script>
    $(document).ready(function ()
    {
        //Dropdownlist Selectedchange event 
        $("#selected_app_ID").change(function () {
            var id = $('#selected_app_ID').val(); // id value
            if (id == 0) {
                $('#selected_job_ID').hide();
            } else {
                $('#selected_job_ID').show();
                $("#selected_job_ID").empty();
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("SelectJobs")',
                    dataType: 'json',
                    data: { id: $("#selected_app_ID").val() },
                    success: function (jobs) {
                        // jobs contains the JSON formatted list of jobs passed from the controller 
                        $("#selected_job_ID").append('<option value=0>*Select a job</option>');
                        $.each(jobs, function (i, job) {
                            $("#selected_job_ID").append('<option value="'
                                + job.job_ID + '">'
                                + job.job_DESC + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve jobs.' + ex);
                    }
                });
            }
            return false;
        });

        //ddl select change
        $("#selected_env_ID").change(function () {
            var name = $('#selected_env_ID option:selected').text(); //Item1
            var id = $('#selected_env_ID').val(); // id value
            if (id == 0) {
                $('#divSubmit').hide();
                $('#selected_app_ID').hide();
                $('#selected_job_ID').hide();
            } else {
                $('#selected_app_ID').show();
            }
        });

        //ddl select change
        $("#selected_job_ID").change(function () {
            var name = $('#selected_job_ID option:selected').text(); //Item1
            var id = $('#selected_job_ID').val(); // id value
            var envid = $('#selected_env_ID').val(); // id value
            if (id == 0) {
                $('#divSubmit').hide();
            } else {
                $('#divSubmit').show();
                alert("envid=" + envid + " jobid=" + id);
            }
        });


    }); // end document ready

</script>

My controller has this and id and envid end up being 0: 我的控制器有这个和id,并且envid最终为0:

public ActionResult Submit(int id = 0,int envid = 0) {

If I need to include something else just let me know. 如果我需要添加其他内容,请告诉我。

Here is the method that fills the job dropdown list. 这是填充作业下拉列表的方法。 This works without issues. 这没有问题。 It's the Html.ActionLink call to Submit that fails to include the parameters. 是对Submit的Html.ActionLink调用,未能包含参数。

public JsonResult SelectJobs(int id)
{
    db.Configuration.ProxyCreationEnabled = false;
    IEnumerable<t_job> jobs = db.t_job.Where(j => j.app_ID == id).ToList();
    return Json(jobs);
}

您需要使用JSON.stringify()

data: JSON.stringify({ id: $("#selected_app_ID").val() }),

Your link 您的连结

@Html.ActionLink("Submit", "Submit", new { id =  Model.selected_job_ID, envid = Model.selected_env_ID }, new {id = "lnkSubmit" })

is rendered on the server side before you make any selection in the dropdowns. 在下拉菜单中进行任何选择之前,将在服务器端呈现。 If the initial values of selected_job_ID and selected_env_ID are zero or null, then those values will be passed to the controller (have a look at the rendered html). 如果selected_job_IDselected_env_ID的初始值为零或null,那么这些值将被传递给控制器​​(请查看呈现的html)。

If you want to pass the values selected in you drop downs, you could either modify the links href attribute in the drop down change events, or create a button instead of a link, and do a redirect in the buttons click event based on the dropdown values. 如果要传递下拉菜单中选择的值,则可以在下拉更改事件中修改links href属性,或者创建按钮而不是链接,然后根据下拉菜单在按钮click事件中进行重定向价值观。

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

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