解决了:我所做的就是将DataTextField和DataValueField更改为“Type”,当我返回.val()时,它给了我与文本完全相同的字段. 我试图从使用脚本的Kendo下拉列表的选定索引的视图中向Controller返回一
我试图从使用脚本的Kendo下拉列表的选定索引的视图中向Controller返回一个值.
到目前为止,我只能让它返回索引值而不是文本.
边问题:另外,在当前上下文中,distinct不起作用,如果我从视图中删除.DataValueField以及从我的DTO和Controller中取出Product ID,我可以得到.DataTextField的不同值(“Type” ).
主要问题:是名称:$(“#typeDropDownList”).val()只抓取ProductID(就像它应该的那样),如果我尝试通过.text()来抓取它,而不是它给了我一个“”(空白字符串)每次都是结果.我想知道如何只抓取下拉列表的文本值.
它也将更新Kendo Grid.
我是一个相当陌生的编程人员,而且我正处于工作阶段,请放轻松.
这是我的观点
<h2>Products</h2>
@(Html.Kendo().DropDownList()
.Name("typeDropDownList")
.HtmlAttributes(new { style = "width: 250px" })
.DataTextField("Type")
.DataValueField("ProductID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetList", "AdvSlider")
.Data("refresh");
})
.ServerFiltering(true);
})
.OptionLabel("Select a type")
.Events(e => e.Change(
@<text>
function(e){
$("#grid").data("kendoGrid").dataSource.read();
}</text>
))
)
@(Html.Kendo().Grid<SKLiqPOC.Models.PatDTO>()
.Name("grid")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.ServerOperation(false)
.Read(read => read.Action("GetJson", "AdvSlider")
.Data("refresh"))
// Set the action method which will return the data in JSON format
.PageSize(20)
)
.Columns(columns => {
//columns.Bound("Name");
columns.Bound(a => a.ProductID).Filterable(false).Width(100);
columns.Bound(a => a.Description);
columns.Bound(a => a.ProductName).Width(160);
columns.Bound(a => a.Manufacturer).Width(160);
columns.Bound(a => a.Price).Format("{0:C}").Width(130);
})
.Pageable() // Enable paging
.Sortable() // Enable sorting
.Filterable() //Enable filtering
.Scrollable()
.HtmlAttributes(new { style = "height:430px;" })
)
<script>
function refresh()
{
return {
name: $("#typeDropDownList").val()
};
};
</script>
这是我的控制器
namespace SKLiqPOC.Controllers
{
public class AdvSliderController : Controller
{
private SKLIQContext db = new SKLIQContext();
public ActionResult Index()
{
return View();
}
public ActionResult GetList()
{
IQueryable<ddlDTO> dtoQuery = (
from name in db.t_prdct_fmly
join product in db.t_prdct on name.prdct_fmly_sys_key_id equals product.prdct_fmly_sys_key_id
join nameCat in db.t_prdct_fmly_ctgy on name.prdct_fmly_sys_key_id equals nameCat.prdct_fmly_sys_key_id
join type in db.t_prdct_ctgy on nameCat.prdct_ctgy_sys_key_id equals type.prdct_ctgy_sys_key_id
where !product.excl_fr_srch
select new ddlDTO
{
Type = type.shrt_descr,
ProductID = product.prdct_fmly_sys_key_id
}).Distinct();
return Json(dtoQuery, JsonRequestBehavior.AllowGet);
}
public ActionResult GetJson([DataSourceRequest]DataSourceRequest request, string name)
{
IEnumerable<PatDTO> dtoQuery = GetItems(name);
if (dtoQuery.Count() <= 0) //If no items are found add a message in the grid stating such
dtoQuery = new PatDTO[1] { new PatDTO { ProductName = "No Items Found" } };
DataSourceResult result = dtoQuery.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
public IQueryable<PatDTO> GetItems(string query)
{
IQueryable<PatDTO> dtoQuery =
from product in db.t_prdct
join name in db.t_prdct_fmly on product.prdct_fmly_sys_key_id equals name.prdct_fmly_sys_key_id
join type in db.t_prdct_ctgy on product.prdct_typ_cd equals type.prdct_ctgy_sys_key_id
join country in db.t_cntry_orig on product.cntry_orig_cd equals country.cntry_orig_sys_key_id
join region in db.t_regn_orig on product.regn_orig_cd equals region.regn_orig_sys_key_id
where !product.excl_fr_srch
&& type.shrt_descr.ToLower().Contains(query.ToLower())
select new PatDTO
{
ProductID = product.prdct_fmly_sys_key_id,
Price = product.retl_price,
ProductName = product.t_prdct_fmly.prdct_fmly_nm,
Manufacturer = name.mnfct_nm,
Description = name.mrkt_nrtv
};
return dtoQuery;
}
}
}
还有我的DTO
namespace SKLiqPOC.Models
{
public class ddlDTO
{
public string Type { get; set; }
public int ProductID { get; set; }
}
}
您可以通过
text属性获取文本值,如下所示:
var selectedText = $("#typeDropDownList").data("kendoDropDownList").text()
