jQuery下拉列表不会返回正确的选择

时间:2013-12-21 14:12:02

标签: c# jquery asp.net-mvc html-select

有谁知道为什么会这样?从我的控制器(FirmsController)我只是试图让选定的公司回到控制器方法(EditAffiliations(string selectedFirm))作为参数。选定的公司价值应该来自下拉列表。但不管我做什么,我似乎无法做到这一点。我是新人,我会承认,但这很荒谬。以下是方法。

public ActionResult EditAffiliations(string selectedFirm)
    {
       // selectedFirm = "8";
        //All available firms
        //Collection<Firm> firms = new Collection<Firm>();
        //Collection<FirmSelectorModel> models = new Collection<FirmSelectorModel>();

        var model = new FirmSelectorModel();
        _workContext.CurrentUser.Firms = _firmService.GetAllFirms().Where(m => m.ExternalId.Contains("F")).ToArray();

        if (selectedFirm==null || _workContext.CurrentFirm == null)
        {
            _workContext.CurrentFirm = _workContext.CurrentUser.Firms.FirstOrDefault();
            model.CurrentFirm = _workContext.CurrentFirm.ToModel();
        }
        else
        {
            model.CurrentFirm = _firmService.GetFirmById(Convert.ToInt16(selectedFirm)).ToModel(); //_workContext.CurrentFirm.ToModel();
        }

        model.AvailableFirms = (_firmService.GetAllFirms().Where(m => m.ExternalId.Contains("F"))).Select(x => x.ToModel()).ToList();

        return View(model);
    }

以下是我的观点:

@model FirmSelectorModel
@using System.Globalization;
@{
var gridPageSize = EngineContext.Current.Resolve<Incendo.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}

@T("Admin.Users.UserAffiliations")

@if (Model.AvailableFirms.Count > 0)
{
using (Ajax.BeginForm("EditAffiliations", "Firms", new AjaxOptions { OnComplete = "onFirmChangedRequestCompleted();" }, new { id = "form-firmselector", role = "form"}))
{
    var firms = Model.AvailableFirms.Select(x => new SelectListItem
    {
        Text = String.Format("{0} - {1}", x.ExternalId, x.Name),
        Value = x.Id.ToString(),
        Selected = x.Id.Equals(Model.CurrentFirm.Id)
    });

    @Html.DropDownList("userfirm",
        firms,
        new { onChange = "onFirmChanged();", @class = "form-control"})
}
<script type="text/javascript">
    function onFirmChanged() {
        //var singleValues = $("#userfirm").val();
        //$("#form-firmselector").submit();
        //alert(singleValues);


    }
    function onFirmChangedRequestCompleted() {
        //location.reload();
    }

    $("select").change(displayVals);
    displayVals();
</script>
}

我希望dropdwonlist中选择的值返回给控制器。我应该在函数onFirmchanged()中写什么?如何将其发送回控制器? 我错过了什么?希望有人会在我失去理智之前回答这个烦恼...... 此致,SM

2 个答案:

答案 0 :(得分:0)

检查您的HTML标记:

<option value="cat">dog</option>
<option value="dog">cat</option>

应该是:

<option value="dog">dog</option>
<option value="cat">cat</option>

狗是狗,猫是猫......

答案 1 :(得分:0)

选中 JSFiddle

<option value="dog">dog</option>
<option value="cat">cat</option>