更新表时dropdownoption变化 [英] Update table when dropdownoption changes

查看:138
本文介绍了更新表时dropdownoption变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其价值取决于我的DropDownList价值的一部分。我怎样才能更新表时的DropDownList的变化选择的选项。

电网

  @foreach(VAR选择在哪里ViewData.Model.Options == choiceId ViewData.Model.choiceLevelId)
                    {
                        < TR类='gridrow'>
                            < TD> .....

下拉菜单

 < D​​IV CLASS =主编场>
                @ Html.DropDownListFor(型号=> model.choiceLevelId,
                            (
                             ...


解决方案

你说的是什么表呢? ASP.NET MVC没有表或椅子的知识。它的工作原理与模型,控制器和视图。

所以,你必须在你所呈现的℃的视图;选择> 框,您希望在用户改变这个选择框的值来调用控制器操作新选择的值传递给服务器,以便它可以进行一些处理。我说得对不对?

您将不得不使用JavaScript在您的视图,以便订阅此下拉列表的更改事件。然后你有几个可能的原因:


  • 使用AJAX请求到选定值发送给服务器。

  • 重定向当前浏览器(使用 window.location.href )来传递给它的选择的值作为操作参数的控制器动作。

根据您的要求,你可以挑选套件最适合你的技术。

让我说明与使用jQuery的例子第一种方式:

 <脚本类型=文/ JavaScript的>
    $(函数(){
        //订阅下拉菜单的.change事件
        $('选择')。改变(函数(){
            //获取新选定的值
            VAR了selectedValue = $(本).VAL();
            //把它作为一个Ajax请求一些控制器动作
            .post的$('@ Url.Action(富)',{值:}了selectedValue,功能(结果){
                //这将在AJAX调用成功的情况下被调用
                // TODO:做控制器操作返回的结果的东西
            });
        });
    });
< / SCRIPT>

和控制器的动作将处理Ajax请求:

  [HttpPost]
公众的ActionResult美孚(字符串值)
{
    //当用户更改选择这个动作将被调用
    //在下拉列表中的一个,并通过了新选择的值。
    //因此,使用这种新的值可以执行所需的处理
    //你的模型在这里,并返回一个视图。由于在调用这个动作
    //使用AJAX调用,你可以一个JSON字符串返回到客户端指示
    //任何处理你打算进行的成功或失败
    // 这里    ...}


更新:

如果你想提交表单包含下拉菜单选择何时更改,可以使用下列内容:

 <脚本类型=文/ JavaScript的>
    $(函数(){
        //订阅下拉菜单的.change事件
        $('选择')。改变(函数(){
            //触发提交含有形式为:
            $(本).closest(形式)提交();
        });
    });
< / SCRIPT>

I have a portion of a table whose values depend on the value in my dropdownlist. How can I update the the table when the selected option of the dropdownlist changes.

grid

@foreach (var choice in ViewData.Model.Options where choiceId == ViewData.Model.choiceLevelId)
                    {
                        <tr class='gridrow'>
                            <td>.....

dropdown

 <div class="editor-field">
                @Html.DropDownListFor(model => model.choiceLevelId,
                            (
                             ...

解决方案

What table are you talking about? ASP.NET MVC has no knowledge of tables or chairs. It works with Models, Controllers and Views.

So you have a View in which you have rendered a <select> box and you want upon changing the value of this select box by the user to invoke a Controller Action passing the newly selected value to the server so that it can perform some processing. Am I right?

You will have to use javascript in your View in order to subscribe to the change event of this dropdown. Then you have a couple of possibilities:

  • Use an AJAX request to send the selected value to the server.
  • Redirect the current browser (using window.location.href) to the controller action passing it the selected value as action argument.

Depending on your requirements you could pick the technique that suites best for you.

Let me illustrate the first approach with an example using jQuery:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // fetch the newly selected value
            var selectedValue = $(this).val();
            // send it as an AJAX request to some controller action
            $.post('@Url.Action("Foo")', { value: selectedValue }, function(result) {
                // this will be invoked in the case of success of the AJAX call
                // TODO: do something with the results returned by the controller action
            });
        });
    });
</script>

and your controller action that will handle the AJAX request:

[HttpPost]
public ActionResult Foo(string value)
{
    // this action will be invoked when the user changes the selection
    // in one of the dropdown lists and passed the newly selected value.
    // So using this new value you could perform the desired processing
    // on your model here and return a view. Since this action was invoked
    // with an AJAX call you could return a JSON string to the client indicating
    // the success or failure of whatever processing you were intending to perform 
    // here

    ...

}


UPDATE:

If you wanted to submit the form containing the dropdowns when the selection changes you could use the following:

<script type="text/javascript">
    $(function() {
        // subscribe to the .change event of the dropdowns
        $('select').change(function() {
            // trigger the submission of the containing form:
            $(this).closest('form').submit();
        });
    });
</script>

这篇关于更新表时dropdownoption变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆