0

我的表单中有多个文本框,它们使用某个 Web-Api 发送 ajax 请求。但是,当我将一个列表框添加到我的控件并从中选择一个项目时,post api 控制器的参数采用空值,但是当我从列表框中取消选择该项目时,它会正确传递 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; }

用户社区标志.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);
        }


    }

阿贾克斯请求

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>

提琴手

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

0 回答 0