简体   繁体   English

级联DDL剃须刀传递值

[英]Cascading DDL razor pass values

What I am trying to do is a simple cascading DDL using my database. 我想做的是使用数据库进行简单的级联DDL。 Say I have an object class: 说我有一个对象类:

public class ObjectModel
{
    public int id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
}

The types are- 'factories', 'samples' and 'sampleresults'. 类型为“工厂”,“样本”和“样本结果”。 The moment a user chooses factories- for example- all factories' names are shown. 用户选择工厂的那一刻-例如,将显示所有工厂的名称。 My Task class has ObjectID member, and an ObjectType member. 我的Task类具有ObjectID成员和ObjectType成员。 So I'd like to pass to ObjectType member the second option while ObjectID gets the first option. 所以我想将第二个选项传递给ObjectType成员,而ObjectID获得第一个选项。 Here is the script I use: 这是我使用的脚本:

    <script type="text/javascript">
    $(function () {
        $.getJSON("/Home/Object/List", function (data) {
            var items = "<option>---------------------</option>";
            $.each(data, function (i, object) {
                items += "<option value='" + object.Value + "'>" + object.Text + "</option>";
            });
            $("#Objects").html(items);
        });

        $("#Objects").change(function () {
            $.getJSON("/Home/ObjectType/List/" + $("#Objects > option:selected").attr("value"), function (data) {
                var items = "<option>---------------------</option>";
                $.each(data, function (i, objectType) {
                    items += "<option value='" + objectType.Value + "'>" + objectType.Text + "</option>";
                });
                $("#ObjectType").html(items);
            });
        });
    });
</script>

My view is: 我的看法是:

   @Html.LabelFor(model => model.ObjectID) 
   <select id="Objects" name="Objects"></select>
    <br />

    @Html.LabelFor(model => model.ObjectName) 
    <select id="ObjectType" name="ObjectType"></select>

My controller is: 我的控制器是:

public ActionResult Create() { return View(); } 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Task task) { 
      if (ModelState.IsValid) { 
         db.Tasks.Add(task); 
         db.SaveChanges(); 
         return RedirectToAction("Index"); } 
      return View(task); 
} 

So it all works fine, and each time I drop the first list down I get all types, then the second DDL shows the relevant names. 因此,一切正常,每次我将第一个列表放下时,我都会得到所有类型,然后第二个DDL显示相关名称。 The only problem is I have no clue how to pass these values to my model class (Task class). 唯一的问题是我不知道如何将这些值传递给我的模型类(任务类)。 I am used to use @Html.dropdownlist but it won't work this time I guess, because I'm using script. 我曾经使用@ Html.dropdownlist,但是我猜这一次不会起作用,因为我正在使用脚本。

Ok lets get the values of those dropdowns first 好吧,让我们先获取那些下拉菜单的值

var obj = $('#Objects').val();
var objType = $('#ObjectType').val();

Lets say your controller action is TaskAction, we pass those values over as get parameters 假设您的控制器动作是TaskAction,我们会将这些值作为get参数传递

$.get("/Home/Controller/TaskAction", { object : obj, ObjectType : objType}, function (data) {

    //Do something after
});

and lets assume your Task class is like this 并假设您的Task类是这样的

public class Task
{

    public string Object { get; set;}
    public string ObjectType { get; set;}


}

You can then model bind it like this 然后可以像这样绑定模型

public ActionResult TaskAction(Task myTask)
{

}

or explicitly pass it over like this 或像这样显式地将其传递

public ActionResult TaskAction(string object, string objType)
{
    new Task{ Object = object, ObjectType = objType}
}

Hope this helps 希望这可以帮助

As long as the name attribute of a form element matches the name of a public property on your model, the ModelBinder will be happy. 只要name的表单元素的属性,在模型上的公用属性的名称相匹配,该模型绑定器会很高兴。 Note that case matters, and it must be spelled exactly the same. 请注意,大小写很重要,并且拼写必须完全相同。 So if your property is color , then name="Colors" will not bind correctly because of both the capital C and the extra s. 因此,如果您的属性是color ,则由于大写字母C和多余的s,因此name="Colors"将无法正确绑定。

So if you want to bind directly to a Task with your existing HTML, Task must look like this: 因此,如果要使用现有的HTML直接绑定到Task ,则Task必须如下所示:

public class Task() {
   public Task() { }
   //need a default constructor with no parameters for model binding to work 

   public string Objects { get; set; }
   //property name "Objects" matches value of input/select/textarea's name attribute

   public string ObjectType { get; set; }

   //other methods / properties as needed...
}

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

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