为什么 Kendo ComboBoxFor 显示 0 而不是 属性 从另一个视图传递的绑定模型?

Why Kendo ComboBoxFor show 0 instead of property binded model which was passed from another view?

我是 ASP MVC 新手。 我对 Kendo ComboBoxFor 有疑问。我尝试了来自 Stack 的数十个示例,但没有成功。

我想将两个 id 从一个视图传递到另一个视图,并在两个 ComboboxFor 中使用它们。在这两个视图中,我都使用相同的模型。 下面是我的两个目标id。

型号

public class Wymiarowanie_PorownajModel
{
    public int Target1 { get; set; }
    public int Target2 { get; set; }
}

这是我在目标视图中绑定此 ID 的两种方法。两者都不起作用。 ComboBoxFor 显示 0 而不是 属性 从另一个视图传递的绑定模型。绑定到 ComboboxFor 的列表可以正常工作。

查看

@model WebApp.Models.Wymiarowanie_PorownajModel
<td class="align_center">
            @(Html.Kendo().ComboBoxFor(model => model.Target1)
            .DataTextField("Opis")
            .DataValueField("Id")
            .Suggest(true)
            .Name("Target1")
            .Filter("contains")
            .BindTo((System.Collections.IEnumerable)ViewData["opis"])
            .Placeholder("Wybierz tłoka bazowego...")
            .HtmlAttributes(new { style = "width:225px;" })
            )
        </td>
        <td class="align_center">
            @(Html.Kendo().ComboBoxFor(model => model.Target2)
            .DataTextField("Nazwa").DataValueField("Id")
            .Suggest(true)
            .Filter("contains")
            .Placeholder("Wybierz opcję 1...")
            .HtmlAttributes(new { style = "width:225px;" })
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("PodajRysunek", "Wymiarowanie_PorownajWymiaryTlokow");
                })
                                 .ServerFiltering(true);
            })
            )
        </td>

控制器

public class Wymiarowanie_PorownajWymiaryTlokowController : Controller
{
  private MainDb db = new MainDb();
  Wymiarowanie_PorownajModel wym = new Wymiarowanie_PorownajModel();

  public ActionResult Index(Wymiarowanie_PorownajModel wymMain)
  {
     wymMain = wym;
     UzupelnijNrRysunku(); 
     return View(wym);
  }

  private void UzupelnijNrRysunku()
  {
     var typ = db.Specyfikacja_V_TlokiZwymiarowaneDoPorownaniaNrRysunku_list
     .Select(c => new
     {
       Id = c.Id,
       Opis = c.Rysunek_Nr_Zmiana
     })
     .OrderBy(e => e.Opis);
     ViewData["opis"] = typ.OrderBy(e => e.Opis);
     ViewData["defaultopis"] = typ.First().Opis;
  }

  public JsonResult PodajRysunek(string text)
  {
      var rysunki =   
      db.Specyfikacja_V_TlokiZwymiarowaneDoPorownaniaNrRysunku_list
      .Select(tlok => new {
         Id = tlok.Id,
         Nazwa = tlok.Rysunek_Nr_Zmiana
      });

      if (!string.IsNullOrEmpty(text))
      {
         rysunki = rysunki.Where(p => p.Nazwa.Contains(text));
      }
      return Json(rysunki, JsonRequestBehavior.AllowGet);
  }
}

我不知道哪里出了问题。 你愿意帮我找到这个错误的解决方案吗? 我希望我的代码对你来说很清楚对不起我的英语。

亲切的问候

这里有绑定数据的例子

   public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
    using (var northwind = new NorthwindEntities())
    {
        IQueryable<Product> products = northwind.Products;
        DataSourceResult result = products.ToDataSourceResult(request);
        return Json(result);
    }
}

您可以找到更多信息here

解决方案解释:我犯了菜鸟的错误。我搞砸了控制器中的实例。我想使用两个单独的视图和两个单独的控制器。

我清理了一个简化的代码并将其放入一个控制器。

我有这个相同的型号 class。最后,我的 ComboBoxFor 如下所示:

@(Html.Kendo().ComboBoxFor(model => model.Target)
    .DataTextField("Nazwa").DataValueField("Id")
    .Suggest(true)
    .Filter("contains")
    .Placeholder("Wybierz opcję 1...")
    .HtmlAttributes(new { style = "width:225px;" })
    .DataSource(source =>
        {
            source.Read(read =>
            {
                read.Action("PodajRysunek", "Wymiarowanie_Porownaj");
            })
                             .ServerFiltering(true);
        })
            )

现在我只使用 JsonResult PodajRysunek 方法将列表绑定到组合框。

我为第二个视图更改了一些方法,该视图由按钮调用。

public ActionResult WymiaryTlokow(Wymiarowanie_PorownajModel wymiarowaniePorownajModel)
    {
        this.wym = wymiarowaniePorownajModel;
        return View(wym);
    }

学不会迟。 :)