简体   繁体   English

ASP.Net MVC 3下拉列表

[英]ASP.Net MVC 3 Drop Down List

I am developing an ASP.Net MVC 3 Web Application. 我正在开发一个ASP.Net MVC 3 Web应用程序。 One of my Razor Views contains a few Textboxes and a Drop Down List. 我的一个“剃刀视图”包含一些文本框和一个下拉列表。 When the User selects an option from the Drop Down List, I need a Partial View, or something like this, to appear below the Drop Down List preferable without a post back. 当用户从下拉列表中选择一个选项时,我需要一个局部视图或类似的东西出现在下拉列表下方,最好不要回发。

The thing is, it isn't as easy as a simply JQuery Hide and Show for the Partial View, when the User selects an option from the Drop Down List, I need their option to be sent to a method in the Controller, perform some logic based on this option, and then return some data to the Partial View. 事实是,这不像部分视图的简单JQuery隐藏和显示那样简单,当用户从下拉列表中选择一个选项时,我需要将其选项发送到Controller中的方法,并执行一些操作。逻辑基于此选项,然后将一些数据返回到部分视图。

I haven't really got much experience with AJAX, but I get the feeling this is the technology I need to use in order to fulfil my problem. 我实际上对AJAX并没有太多的经验,但是我感到这是我为了解决问题而需要使用的技术。

Has anyone ever had to code anything similar to what I have described above? 有没有人不得不编写类似于我上面描述的内容的代码? And if so, is AJAX what I need to use? 如果是这样,我需要使用AJAX吗?

Also, if anyone knows of any tutorials or code snipets I could look at to help, that would be greatly appreciated. 另外,如果有人知道我可以帮助的任何教程或代码片段,将不胜感激。

Thanks. 谢谢。

UPDATE 更新

I have followed Ryan's answer, but unfortunately I am still having some problems. 我遵循了瑞安的回答,但是不幸的是我仍然遇到一些问题。 I have created the following JavaScript file which is then referenced in my View 我创建了以下JavaScript文件,该文件随后在我的视图中被引用

$(document).ready(function () {

$("#myDDL").change(ChangeEventOfDDL);

function ChangeEventOfDDL(){
var dropDownValue = $('#myDDL').val();
//alert(dropDownValue);
$.ajax({ type: "GET",
       url: '@Url.Action("SomePartialView","testAjax")',
       data: {
           id: dropDownValue
       },
       success: function(data) {
             $('#someDivToLoadContentTo').html(data);
       }
    });
}

});

View 视图

    <select id="myDDL">
    <option></option>
    <option value="1">F1</option>
    <option value="2">F2</option>
    <option value="3">ST1</option>
    <option value="4">ST2</option>
    </select>


<div id="someDivToLoadContentTo">

</div>

My Controller then looks like this 然后我的控制器看起来像这样

public class testAjaxController : Controller
{
    //
    // GET: /testAjax/

    LocumEntities context = new LocumEntities();

    public ActionResult SomePartialView(int id)
    {
        var test = "Hello World";

        return View(test);
    }
}

However, my method 'SomePartialView' never gets hit. 但是,我的方法'SomePartialView'从未被使用过。 Does anyone know why? 有人知道为什么吗?

Thanks. 谢谢。

Yes Ajax would be the easiest thing to use here. 是的,在这里使用Ajax是最简单的方法。 There are plenty of good Ajax tutorials around, just remember that what Ajax does is effectively a background POST, so you can do everything you would normally do with MVC, except within an existing page. 周围有很多不错的Ajax教程,只记得Ajax所做的实际上是后台POST,因此,除了现有页面之外,您可以执行通常使用MVC所做的所有事情。

The way I would get this to work would be to have your code something like this: 我要使其工作的方法是使您的代码如下所示:

public class SomeController{
    public ActionResult SomePartialView(){
         // Do some logic
         return View("SomePartial");
    }
}

Your Ajax would be something like: 您的Ajax类似于:

function ChangeEventOfDDL(){
    $.ajax({
           url: '@Url.Action("SomePartialView","Some")',
           success: function(data) {
                 $('#someDivToLoadContentTo').html(data);
           }
    });
}

I hope that helps at least a little bit. 我希望至少能有所帮助。

Crucially with Ajax, you can add a data object to the function, which is passed as a querystring. 对于Ajax而言,至关重要的是,您可以向该函数添加一个数据对象,该对象作为查询字符串传递。 This means you can send values from your page quite easily with ajax. 这意味着您可以使用ajax轻松地从页面发送值。 Working with the above example, the Javascript would be: 使用上面的示例,JavaScript将是:

function ChangeEventOfDDL(){
    var dropDownValue = $('#ddl').val();
    $.ajax({
           url: '@Url.Action("SomePartialView","Some")',
           data: {
               id: dropDownValue
           }
           success: function(data) {
                 $('#someDivToLoadContentTo').html(data);
           }
    });
}

The Id value is linked with the parameters of the method in your MVC class: Id值与MVC类中方法的参数链接:

public class SomeController{
    public ActionResult SomePartialView(int id){
         // Do some logic 
         var model = MakeModelWithSupplierId(id);
         return View("SomePartial",model);
    }
}

And there you have an interactive partial view that has been populated with the value from your drop down. 在那里,您有一个交互式的局部视图,其中已填充了下拉菜单中的值。

Since a controller can also return a partial view, you can do the following: 由于控制器还可以返回局部视图,因此您可以执行以下操作:

$('#idofyourdropdown').change(function () {
 var theValue = $(this).val();

  $.post('@Url.Action("Action","Controller")', {nameOfParameter: theValue, function(data) {
     $('#divWhereYouWantToAttachData').html(data);
  });
});

On the change event of your dropdown, send the selected value to your desired controller action, which will pass it to the partial view and return the rendered html. 在下拉菜单的change事件中,将选定的值发送到所需的控制器操作,该操作将其传递到部分视图并返回呈现的html。 The html is received in the data var and can be attached to the dom, wherever you want it (see jQuery documentation for this). 该html会在data变量中接收,并且可以随您所需的位置附加到dom(有关此信息,请参阅jQuery文档)。

This is a common use case. 这是一个常见的用例。

What you need to do is 您需要做的是

  • create a controller method that can take the parameters you need (read about MVC model binding before you do so) 创建一个控制器方法,该方法可以采用您需要的参数(在这样做之前,请阅读有关MVC模型绑定的信息)
  • write javascript that will harvest the data you want from your form and make a call to your new controller method and render the results below 编写JavaScript,它将从表单中获取所需的数据,并调用新的控制器方法,并在下面呈现结果

In jQuery it goes something like this: 在jQuery中,它是这样的:

$("#yourDIV").load('/area/controller/method', { property1: 'asasd', property2: 'asdasdadfa'})

The second parameter of this call should be prepared based on the data you harvest from your form. 该调用的第二个参数应根据您从表单中收集的数据来准备。 (if you don't know how, then learn javascript) (如果您不知道如何,请学习javascript)

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

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