How to Pass Kendo DropDownList DataTextField value to Controller
Asked Answered
C

1

0

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);
}
Considered answered 20/6, 2015 at 8:40 Comment(10)
@AmirHosseinMehrvarzi Selam Hossein. Any reply please?Considered
If you're using DataTextFields rather than DataValueFields on the controller, so why you don't enumerate DataTextFields like DataValueFields?Patrick
@AmirHosseinMehrvarzi Could you post a sample code please? I have really tried many ways, but I think I made mistakes and not know how to solve this?Considered
A <select> only post back one element, so do you want the selected Projects ID or Description property to be posted?Bechuanaland
For the first operation `Description' is enough for me as I will display it on the next View. However, the next View is a create view and I will need the ID values for passing to the Controller. What do you suggest?Considered
Then you need to post the ID property (as your doing now). If you also need Description in the POST method, then you should call the database again.Bechuanaland
@StephenMuecke Thanks for your help again. Why I had to use two different View such a kind of operation is that: I cannot populate the model values in case there is a problem on the controller and view returns from the controller. So, to solve this I used Ajax, but in that case I cannot pass File Attachments. Finally I have to apply this approach :(Considered
@StephenMuecke I only call database once. The first time I just pass the Description value to display it on the next view.Considered
Not sure I fully understand, but you can use ajax to upload files - refer this answerBechuanaland
@StephenMuecke For Ajax upload that answer is really very helpful. Thanks a lot.Considered
P
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
Patrick answered 20/6, 2015 at 8:58 Comment(3)
Thanks for answer. But I think I will also need DataValueField for the next operation so that I pass the id for creating new record. So, will it solved that problem as well?Considered
So do you need both values?Patrick
Yes. Actually issueViewModel retains the DataValueField values, but I am not sure if it still retains after applying the changes you suggested. Shall I try?Considered

© 2022 - 2024 — McMap. All rights reserved.