选择列表框项时,控制器将采用null参数

时间:2013-04-28 02:28:47

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 knockout.js asp.net-web-api

我的表单中有多个文本框,它们使用某个Web-Api发送ajax请求。但是,当我向我的控件添加一个列表框并从中选择一个项目后,api控制器的参数取值为null,但是当我从列表框中取消选择它正确传递json数据时。我怎么能解决这个问题呢?

UserCommunityTemp.cs

    [Key]
    public int GroupId { get; set; }


    [MaxLength(1)]
    [DisplayName("Grup Tipi")]
    public string GroupType { get; set; }

    [Required]
    [DisplayName("Grup Adı")]
    public string GroupName { get; set; }

    [Required]
    [DisplayName("Web Adresi")]
    public string GroupUrl { get; set; }

    [Required]
    [DisplayName("E-posta")]
    public string GroupEmail { get; set; }

    [Required]
    [DisplayName("Telefon")]
    public string GroupPhone { get; set; }

    [Required]
    [DisplayName("Adres")]
    public string GroupAddress { get; set; }

    [Required]
    [MaxLength(1)]
    [DisplayName("Grup Türü")]
    public string GroupPrivacy { get; set; }

    public int UserId { get; set; }

    public DateTime CreationDate { get; set; }

    public virtual ICollection<UserCommunitySigns> Signs { get; set; }

UserCommunitySigns.cs

[Key]
    public int SignId { get; set; }

    public int UserId { get; set; }

    public virtual UserCommunityTemp CommunityTemp { get; set; }

控制器操作

public HttpResponseMessage PostUserCommunityTemp(HomeVM usercommunitytemp)
    {
        if (ModelState.IsValid)
        {
            usercommunitytemp.CommunityTemp.GroupType = "2";
            usercommunitytemp.CommunityTemp.CreationDate = DateTime.Now;
            usercommunitytemp.CommunityTemp.UserId = WebSecurity.CurrentUserId;
            db.UserCommunityTemps.Add(usercommunitytemp.CommunityTemp);
            db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, usercommunitytemp);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = usercommunitytemp.CommunityTemp.GroupId }));
            return response;
        }
        else
        {
           return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }


    }

Ajax请求

function UserCommunityViewModel() {
    var self = this;
    self.requests = ko.observableArray();

    var baseUriUserCommunity = '@ViewBag.ApiUrlUsCom';
    var listData;
    $('#CommunityTemp_Signs').each(function () {
        listData=$(this).val();
    });
    self.create = function (formElement) {
            $(formElement).validate();
            if($(formElement).valid()){
                $.post(baseUriUserCommunity,$(formElement).serialize(), null, "json")
                   .done(function (o) {
                       self.requests.push(o);
                       $('#createUserGroup').modal('hide');
                       $('#alert').text("Grup Oluşturuldu.");
                       $('.alert').addClass('alert-success');
                       $('.alert').show()
                       window.setTimeout(function () { $('.alert').alert('close'); }, 5000);
                   })
                    .fail(function () {
                        $('#createGroup').modal('hide');
                        $('#alert').text("İsteğiniz gönderilirken bir hata oluştu lütfen daha sonra tekrar deneyiniz");
                        $('.alert').addClass('alert-error');
                        $('.alert').show()
                        window.setTimeout(function () { $('.alert').alert('close'); }, 5000);
                    });
            }
        }
    }
$(document).ready(function () {
    $("input[data-autocomplete-friends]").each(function () {
        var target = $(this);
        target.autocomplete({
            source: target.attr("data-autocomplete-friends"),
            select: function (event, ui) {
                $('#CommunityTemp_Signs').append('<option value=' + ui.item.id + '>' + ui.item.value + '</option>');
            }
        });
    });
    ko.applyBindings(new UserCommunityViewModel(), document.getElementById("crUserCommunity"));
});

HTML部分

<div class="editor-field">
        <input id="searchFriends" type="text" data-autocomplete-friends="@Url.Action("QuickSearchFriends", "Home")" />
    </div>
    <div class="editor-label">
        <p></p>
    </div>
    <div class="editor-field">
        @Html.ListBoxFor(model=>model.CommunityTemp.Signs,new MultiSelectList(Model.CommunityTemp.Signs))

    </div>

Fiddler

CommunityTemp.GroupType=5&CommunityTemp.GroupName=gfdafsd&CommunityTemp.GroupPrivacy=5&CommunityTemp.GroupUrl=fdasfdsa&CommunityTemp.GroupEmail=gfdasfsd&CommunityTemp.GroupPhone=fasfsda&CommunityTemp.GroupAddress=fdsafs&CommunityTemp.Signs=3

0 个答案:

没有答案