简体   繁体   English

如何立即获取C#MVC中下拉框所选项目的更新值?

[英]How do I instantly get updated value of dropdown box selected item in C# MVC?

I have a list of "practices" that I need to filter on based on user selection. 我有一个基于用户选择需要过滤的“实践”列表。 My view has a dropdown that looks like this: 我的视图有一个下拉列表,如下所示:

@Html.DropDownList("practice", (List<SelectListItem>)ViewBag.PracticeList)

It allows the user to select one of 6 or so practices to filter another list on. 它允许用户从6种左右的方法中选择一种来过滤另一个列表。 I need to retrieve the value and have the page updated. 我需要检索值并更新页面。 I have the page filling with the valued by default without mind to the practice filter, I just can't update it because I can't seem to retrieve the value. 我在页面上填充了默认值,而无需考虑实践过滤器,我只是无法更新它,因为我似乎无法检索到该值。 I just need to get this value so I can incorporate it into my loop to rebuild the list. 我只需要获取此值,即可将其合并到循环中以重建列表。

@foreach (var item in Model)
{      
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.User.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.User.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.User.Email)
    </td>
    <td>
        @Html.TextBox("Hours")
    </td>        
</tr>
}

Start by using jquery to capture a change in the select: 首先使用jquery捕获select中的更改:

$('#selectList').on('change', function() {

})

fiddle 小提琴

then, ajax to a controller that will return your other items: 然后,ajax到一个控制器,该控制器将返回您的其他项目:

$.ajax({
    url: '@Url.Action("Action","Controller")',
    data: { 'selection' : $(this).val() },
    type: "post",
    cache: false,
    success: function (items) {
        //update the other select
    };
});

Using Razor: 使用剃刀:

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions {UpdateTargetId = "DivToUpdate"}))
{
    @Html.DropDownList("practice", (List<SelectListItem>)ViewBag.PracticeList, new { onchange="this.form.submit()"})
}

you could then return the dropdownlist from your controller, or however you want to handle that. 然后,您可以从控制器返回该下拉列表,或者但是要处理该列表。

EDIT 编辑

Actually, I don't think you'll be able to use the Ajax.BeginForm approach as you would have nested forms. 实际上,我认为您不能像嵌套表单那样使用Ajax.BeginForm方法。 You may be stuck with jQuery 您可能会陷入jQuery

You can also use @Ajax.BeginForm and in the action of the controller return partial view and render it on the client side in the success handler of the ajax form. 您还可以使用@ Ajax.BeginForm并在控制器的操作中返回部分视图,并将其呈现在ajax表单的成功处理程序的客户端上。

Here a post on @Ajax.BeginForm 这是@ Ajax.BeginForm上的帖子

Using Ajax.BeginForm in MVC4 在MVC4中使用Ajax.BeginForm

But you will have to have a jquery script that submits the form on your select change event. 但是您将必须具有一个jquery脚本,该脚本会在您的select change事件上提交表单。

You can use AjaxBeginFormfor like this: 您可以像这样使用AjaxBeginFormfor:

@model YourModel
        @using (Ajax.BeginForm("Action", "Controller", null,
                            new AjaxOptions { HttpMethod = "POST",
                                OnSuccess = "functionIfYouWant(data)",
                                 UpdateTargetId = "divid",
                                 InsertionMode=InsertionMode.InsertAfter},
                                new { id = "formId" }))
     {
       @Html.DropDownListFor(c=>c.SelectedVal, (List<SelectListItem>)ViewBag.PracticeList)
    <div id="divId">

    </div>
        <input type="submit" value="Submit"/>
//or add @Html.DropDownListFor(c=>c.SelectedVal, //  List<SelectListItem>)ViewBag.PracticeList,new {onChange="jQuery("#formId").submit()"})
        }

Now you can post model to controller and the returned content will be appended to content of div that have id="divId" Hope you understand what I was trying to explain . 现在,您可以将模型发布到控制器,并且返回的内容将追加到具有id =“ divId”的div内容中,希望您理解我要解释的内容。 Also try to avoid ViewBag in your aplciation is not good practice.Try to use SelectlistItem in your model to populate dropdown. 另外请尽量避免在应用中使用ViewBag不是一个好习惯。请尝试在模型中使用SelectlistItem来填充下拉列表。 so suppose you have your model: 因此,假设您有自己的模型:

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}

In controller you will need to set(populate from db or from somewhere ) your list of DropDownObj. 在控制器中,您需要设置(从db或从某处填充)您的DropDownObj列表。 and in view you will be able to do something like this : 并且鉴于您将能够执行以下操作:

@Html.DropDownListFor(c=>c.SelectedItem,Model.SelectListItemListObj)

This is how mvc dropdownlsit should look in mvc. 这就是mvc dropdownlsit在mvc中的外观。

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

相关问题 如何从MVC C#的下拉列表中获取选定的值 - How to get selected value from dropdown list in mvc c# 我如何从MVC下拉列表中获取选定的值作为C#中的操作链接ID - How can I get selected value from mvc dropdown list as action link id in c# 如何在 C# 中获取下拉列表的选定值 - How to get the selected value of a dropdown in C# 如何从html下拉列表中获取所选项目并将其传递给mvc c#中的另一个控制器 - how to get the selected item from html dropdown list and pass it to another controller in mvc c# 如何获取列表框中所选项目的编号(C#) - How do I get the number of the item selected in a listbox (C#) 如何将组合框的选定项获取到 c# 中的字符串变量 - How to get the selected item of a combo box to a string variable in c# 如何从MVC的下拉菜单中获取所选项目 - how to get the selected item from dropdown in MVC 如何将选定的 DropDownListFor 项目传递给我的 Controller? MVC3 c# - How do I pass a selected DropDownListFor item to my Controller? MVC3 c# 如何从数据网格组合框列中获取所选项目值 - How do I get the Selected Item Value from Data Grid Combo Box Column 如何在c#中获取禁用下拉列表的选定值 - how to get selected value of Disabled dropdown in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM