1

i have a kendodropdown list with the workername and details... how can i fetch the ID of the selected worker so i can able to save... everytime i save it returns a null value in ID.. thanks for those who can help

Heres my code:

<input id="titles" class:"validate[required] inputLong" style="width: 400px;"  />

 $(document).ready(function () {
        var clientCusPosId = $("#clientCusPosId").val();
        $("#titles").kendoDropDownList({
                        dataTextField: "workerName",
                        dataValueField: "workerID",
                        autoBind: false,

                        // define custom template
                        template:
                                      '<h5>${ data.workerName }</h5>' +
                                      '<p>${ data.workerID }</p>' +
                                      '<p>${ data.AvailableDay_LookID }</p>' +
                                      '<p>${ data.StartTime } - ${ data.EndTime }</p>',
                        optionLabel: "Assign worker",

                        dataSource: {
                            transport: {
                                read: {
                                    url: '/Client/LoadWorkerDropdownList?clientCusPosId=' + clientCusPosId,
                                    dataType: "json",
                                    type: "POST"
                                }
                            }
                        }

                    });

                    var dropdownlist = $("#titles").data("kendoDropDownList");
                    dropdownlist.list.width(250);
 });

My Controller:

[Authorize]
[HttpPost]
public ActionResult ClientWorkerPositionSave(FormCollection formCollection)
{
    String msg = String.Empty;
    String clientWorkerPosId = formCollection["clientWorkerPosId"];
    String clientID = formCollection["clientId"];
    String clientCusId = formCollection["clientCusPosId"];
    String workerID = formCollection["titles"];




    Client_Worker_Position clientCusPos = new Client_Worker_Position();
    try
    {
        if (String.IsNullOrWhiteSpace(clientWorkerPosId) || clientWorkerPosId == "0")
        {
            clientCusPos.ClientCustomerPositionID = Convert.ToInt32(clientCusId);
            clientCusPos.WorkerID = Convert.ToInt32(workerID);
            clientCusPos.ClientID = Convert.ToInt32(clientID);

            clientCusPos.DateCreated = DateTime.UtcNow;
            clientCusPos.DateModified = DateTime.UtcNow;
            clientCusPos.CreatedBy = User.Identity.Name;
            clientCusPos.ModifiedBy = User.Identity.Name;

            db.Client_Worker_Position.Add(clientCusPos);
        }
        else
        {
            int id = Convert.ToInt32(clientWorkerPosId);
            clientCusPos = (from a in db.Client_Worker_Position
                            where a.ID == id
                            select a).SingleOrDefault();


            clientCusPos.ClientCustomerPositionID = Convert.ToInt32(clientCusId);
            clientCusPos.WorkerID = Convert.ToInt32(workerID);
            clientCusPos.ClientID = Convert.ToInt32(clientID);


            clientCusPos.DateModified = DateTime.UtcNow;
            clientCusPos.ModifiedBy = User.Identity.Name;
        }
    }
    catch (Exception)
    {
        msg = "Failed to save";
    }

    db.SaveChanges();

    if (String.IsNullOrWhiteSpace((msg)))
    { TempData["message"] = "Saved Successfully."; }
    else if (msg != "")
    { TempData["message"] = msg; }

    return RedirectToAction("ClientCustomerDetails", new { });
}
4

1 回答 1

0

$("#titles).val()因为它已经配置好了,所以它可以像用来获取您的 WorkerID 一样简单 。

创建一个隐藏的输入字段 hidden id="workerID" 然后在您的帖子之前或在下拉更改事件中将其设置为 $("#workerID").val($("#titles).val())。这应该来在您的控制器集合中。

于 2013-08-28T04:12:38.150 回答