如何在我的kendoDropdownlist中获取工作者ID

时间:2013-08-28 02:27:42

标签: asp.net-mvc-3 kendo-ui kendo-asp.net-mvc

我有一个kendodropdown列表,其中包含工人名称和详细信息...如何获取所选工作人员的ID以便我能够保存...每次我保存它都会在ID中返回空值..感谢那些谁可以提供帮助

继承我的代码:

<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);
 });

我的控制器:

[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 { });
}

1 个答案:

答案 0 :(得分:0)

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

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