How to Pass Kendo DropDownList DataTextField value to Controller

喜你入骨 提交于 2019-12-12 02:58:07

问题


I have a Kendo DropDownList on the View and I want to pass its DataTextField value to the Controller and then pass and them on the labels in another View. Although I can pass DataValueField values to the Controller, I cannot pass DataTextField values. I tried to apply different scenarios but I could not. Any idea? On the other hand, if it is not possible, should the DataTextField values be populated again on the Controller and return to the other View?

View:

@model IssueViewModel

...
@Html.LabelFor(m => m.ProjectID)
@(Html.Kendo().DropDownList()
    .Name("ProjectID")
    .DataTextField("ProjectName")
    .DataValueField("ProjectId")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetProjects", "Issue");
        });
    })
)

Controller:

public JsonResult GetProjects()
{
    var projects = repository.Projects;
    return Json(projects.Select(m => new { ProjectId = m.ID, ProjectName = m.Description }), JsonRequestBehavior.AllowGet);
}


/* I want to pass the DataTextField values to this 
method and return them to the CreateManagement view */
public ActionResult Create(IssueViewModel issueViewModel)
{
    return RedirectToAction("CreateManagement", issueViewModel);
}

回答1:


Change your controller to this:

public JsonResult GetProjects()
{
    var projects = repository.Projects;
    return Json(projects.Select(m => new SelectListItem { ProjectId = m.Description, ProjectName = m.Description }).ToList(), JsonRequestBehavior.AllowGet);
}

Since the DropDownList uses DataTextField for the user and uses DataValueField for the server communications, so you have to use DataTextField value for both. Then you can use it for the next operations.

Edit: if you need both values on the controller, change JsonResult method to :

return Json(projects.Select(m => new SelectListItem { ProjectId = m.Description + "," + m.ID , ProjectName = m.Description }).ToList(), JsonRequestBehavior.AllowGet);

Now you can use both in the next operations just by spiting them like:

var _both = value.split(',');//value: returned value from the view


来源:https://stackoverflow.com/questions/30952049/how-to-pass-kendo-dropdownlist-datatextfield-value-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!