我是ASP MVC新手。 我有剑道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);
}
}
我不知道出了什么问题。 你想帮我找到这个错误的解决方案吗? 我希望我的代码对你很清楚抱歉我的英文。
亲切的问候
答案 0 :(得分:0)
这里有绑定数据的示例
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
答案 1 :(得分:0)
解决方案解释:我犯了菜鸟的错误。我弄乱了控制器中的实例。我想用两个独立的控制器来使用两个独立的视图。
我清理了一个缩小的代码并将其放到一个控制器上。
我有同样的模型类。最后,我的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);
}
学习永远不会迟到。 :)