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);
}
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Jack
  • 1
  • 21
  • 118
  • 236
  • @AmirHosseinMehrvarzi Selam Hossein. Any reply please? – Jack Jun 20 '15 at 08:46
  • 1
    If you're using `DataTextField`s rather than `DataValueField`s on the controller, so why you don't enumerate `DataTextField`s like `DataValueField`s? – Amirhossein Mehrvarzi Jun 20 '15 at 08:46
  • @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? – Jack Jun 20 '15 at 08:48
  • 1
    A ` –  Jun 20 '15 at 09:05
  • 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? – Jack Jun 20 '15 at 09:09
  • 1
    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. –  Jun 20 '15 at 09:11
  • @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 :( – Jack Jun 20 '15 at 09:13
  • @StephenMuecke I only call database once. The first time I just pass the Description value to display it on the next view. – Jack Jun 20 '15 at 09:15
  • 1
    Not sure I fully understand, but you can use ajax to upload files - [refer this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Jun 20 '15 at 09:18
  • @StephenMuecke For Ajax upload [that](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) answer is really very helpful. Thanks a lot. – Jack Jun 20 '15 at 10:18

1 Answers1

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
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70