简体   繁体   English

如何将SelectList的选定值传递给我的Action?

[英]How can I pass the selected value of SelectList to my Action?

In my ASP.NET MVC 4 project I have a View that has this piece: 在我的ASP.NET MVC 4项目中,我有一个View有这个部分:

    <td>
         @Html.DropDownList("SelectedValue", ViewBag.SoftwaresList as SelectList, "Add New Software")
    </td>
    <td>
        No Description
    </td>
    <td>
        @Html.ActionLink("Add", "AddSoftware", new { pDeviceId = Model.Id, pSoftwareId =  Model.SelectedValue})
    </td>

My Model has an object property "SelectedValue". 我的模型有一个对象属性“SelectedValue”。 What I am trying to do is, when a user clicks the Link Add, I pass the Device Id (works fine, because this is straight from the Model being passed into the the View) AND the Selected Id from the DropDownList. 我想要做的是,当用户点击链接添加时,我传递设备ID(工作正常,因为这是从传递到视图中的模型直接)和DropDownList中的选定ID。 The DropDownList is a collection of a different Model, but it contains an Id as well. DropDownList是不同Model的集合,但它也包含Id。

I've tried the following with no luck: 我试过以下没有运气:

@Html.ActionLink("Add", "AddSoftware", new { pDeviceId = Model.Id, pSoftwareId = 
      ((ViewBag.SoftwaresList as SelectList).SelectedValue as SoftwareModel).Id})

It's worth noting: The DropDownList is populated with my SoftwareModel's correctly, however, I am having no luck passing that additional value from the selected value to to the Action in the Controller. 值得注意的是:DropDownList正确填充了我的SoftwareModel,但是,我没有运气将所选值中的附加值传递给Controller中的Action。

In fact, what I see in Chrome hover over is: 事实上,我在Chrome浏览器中看到的是:

someURL\\AController\\AddSoftware?pDeviceId=2 someURL \\ AController \\ AddSoftware?pDeviceId = 2

Where I would expect it to be something like: 在哪里我会期望它是这样的:

someURL\\AController\\AddSoftware?pDeviceId=2&pSoftwareId=5 someURL \\ AController \\ AddSoftware?pDeviceId = 2&pSoftwareId = 5

It is easiest to have the list in a form (with @Html.BeginForm or @Ajax.BeginForm) and have the Button be the submit button for that form. 最简单的方法是将表单放在一个表单中(使用@ Html.BeginForm或@ Ajax.BeginForm)并使Button成为该表单的提交按钮。 The controller action method can define an input parameter of the model you are trying to submit or just the ID itself. 控制器操作方法可以定义您要提交的模型的输入参数或仅定义ID本身。 In that case the browser and the framework will do all the work for you. 在这种情况下,浏览器和框架将为您完成所有工作。

@using (
        Ajax.BeginForm
            (
               "MyAction",
                "MyController",
                new {Param1 = value1, param2 = value2 },
                new AjaxOptions
                    {
                        OnSuccess = "javascriptForSuccess",
                        UpdateTargetId = "DivToUpdate",
                        OnFailure = "javascriptForFailure",
                        InsertionMode = InsertionMode.Replace,
                        HttpMethod = "POST",
                    }
            )
        )
{
    <td>
         @Html.DropDownList("SelectedValue", ViewBag.SoftwaresList as SelectList, "Add New Software")
    </td>
    <td>
        No Description
    </td>
    <td>
        <input type="submit" id="Btn_Create" name="Btn_Create" value="Create" />
    </td>
}

You are creating that action link on the server and sending HTML to client. 您正在服务器上创建该操作链接并将HTML发送到客户端。 The user selects a value from drop down on client which means it is not available when you create HTML for your Add link (ie @Html.ActionLink("Add" ) 用户从客户端下拉列表中选择一个值,这意味着当您为Add链接创建HTML时它不可用(即@Html.ActionLink("Add"

You should, instead, create a form and post values to server to get selected value 相反,您应该创建一个form并将值发布到服务器以获取所选值

Or 要么

You can use JavaScript to get selected value from link and send it to server when the link gets clicked. 您可以使用JavaScript从链接中获取所选值,并在单击链接时将其发送到服务器。

尝试这个

@Html.DropDownListFor(m => m.pDeviceId , (SelectList)ViewBag.SoftwaresList, new { onchange = "document.location.href = 'AddSoftware?pDeviceId=' + this.options[this.selectedIndex].value;" })

Your best option is to turn this into a form by using the following HTML and Controller setup. 您最好的选择是使用以下HTML和Controller设置将其转换为表单。 You will also have to change ViewBag.SoftwaresList into a List<SelectListItem>() 您还必须将ViewBag.SoftwaresList更改为List<SelectListItem>()

HTML: HTML:

@using(Html.BeingForm())
{
<td>
     @Html.DropDownListFor(x => x.SelectedValue, ViewBag.SoftwaresList as List<SelectListItem>, "Add New Software")
</td>
<td>
    No Description
</td>
<td>
    @Html.ActionLink("Add", "AddSoftware", new { pDeviceId = Model.Id, pSoftwareId =  Model.SelectedValue})
</td>
}

Controller: 控制器:

[HttpPost]
public ActionResult(MyModel model)
{
    //Logic Code Goes Here
    return View(model);
}

Select List: Here is how you can create a select list. 选择列表:以下是创建选择列表的方法。 Note how I do not use the 'Selected' parameter, this will be automatically calculated for you so there is no need 请注意我不使用'Selected'参数,这将自动为您计算,因此没有必要

var selectList = new List<SelectListItem>();
selectList.Add(new SelectListItem
{
    Text = "Name",
    Value = "ID"
});

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

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