C# 回发后ViewModel未正确填充

C# 回发后ViewModel未正确填充,c#,jquery,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Asp.net Mvc,Asp.net Mvc 4,我的视图中有两个列表框和提交按钮。列表框之间还有箭头按钮,用于将值从左向右移动,反之亦然。使用jqueryclick函数处理值的移位。我关心的是从最右边的listbox控件中检索值。下面是代码 列表框的代码 @Html.ListBoxFor(m => m.SelectedExchangesIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.SelectedExchanges, "Value", "Text"),

我的视图中有两个列表框和提交按钮。列表框之间还有箭头按钮,用于将值从左向右移动,反之亦然。使用jqueryclick函数处理值的移位。我关心的是从最右边的listbox控件中检索值。下面是代码

列表框的代码

@Html.ListBoxFor(m => m.SelectedExchangesIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.SelectedExchanges, "Value", "Text"), new { id = "destinationExchanges", name = "destinationExchanges", @style = "width: 250px;height: 144px;", @CssClass = "MyListBox" })
我还在视图中声明了一个隐藏字段,如下所示

@Html.HiddenFor(m => m.SelectedExchangesIds, "SelectedExchangesIds");
 [HttpPost]
  public ActionResult Edit(SubscriptionViewModel model)
  {
      if (ModelState.IsValid) { }
  }
绑定到视图的视图模型包含该属性

public IEnumerable<string> SelectedExchangesIds { get; set; }
谁能告诉我出了什么问题吗

好的,我正在复制粘贴下面的全部代码

我的观点代码

@model Avanade.Bureau.Subscription.Models.SubscriptionViewModel
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

<link href="~/Content/Site.css" rel="stylesheet" />

@*<script src="~/Scripts/jquery-1.10.2.min.js"></script>*@
<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $('.chkclass').click(function () {

            var getchkid = $(this).attr('id');
            var isChecked = $('#' + getchkid).is(':checked');

            if ($('#' + getchkid).is(':checked') == true) {
                $('#td' + $(this).val()).css("color", "white");
                $('#td' + $(this).val()).css("background-color", "gray");
            }
            else {
                $('#td' + $(this).val()).css("color", "black");
                $('#td' + $(this).val()).css("background-color", "white");
            }
        });


        $('#chk10').click(function (event) {  //on click
            if (this.checked) { // check select status
                $('.chkclass').each(function () { //loop through each checkbox
                    this.checked = true;  //select all checkboxes with class "checkbox1"
                    $('#td' + $(this).val()).css("color", "white");
                    $('#td' + $(this).val()).css("background-color", "gray");
                });
            } else {
                $('.chkclass').each(function () { //loop through each checkbox
                    this.checked = false; //deselect all checkboxes with class "checkbox1"
                    $('#td' + $(this).val()).css("color", "black");
                    $('#td' + $(this).val()).css("background-color", "white");
                });
            }
        });


        $('#sourceUsers').click(function (e) {
            //$("#sourceItems").css("background-color", "gray");
            $('option:selected').css('color', 'white');
            $('option:selected').css('backgroundColor', 'gray');
            //$('#sourceItems option:selected').css("color", "white");
            //$('#sourceItems option:selected').css("background-color", "gray");
            e.preventDefault();
        });

        $('#destinationUsers').click(function (e) {
            //$("#sourceItems").css("background-color", "gray");
            $('option:selected').css('color', 'white');
            $('option:selected').css('backgroundColor', 'gray');
            //$('#sourceItems option:selected').css("color", "white");
            //$('#sourceItems option:selected').css("background-color", "gray");
            e.preventDefault();
        });


        $('#ShiftExchangesRight').click(function (e) {
            $('#sourceExchanges option:selected').appendTo('#destinationExchanges');
            e.preventDefault();
        });

        $('#ShiftExchangesLeft').click(function (e) {
            $('#destinationExchanges option:selected').appendTo('#sourceExchanges');
            e.preventDefault();
        });

        $('#ShiftExchangesRightAll').click(function (e) {
            $('#sourceExchanges option').appendTo('#destinationExchanges');
            e.preventDefault();
        });

        $('#ShiftExchangesLeftAll').click(function (e) {
            $('#destinationExchanges option').appendTo('#sourceExchanges');
            e.preventDefault();
        });


        $('#ShiftUsersRight').click(function (e) {
            $('#sourceUsers option:selected').appendTo('#destinationUsers');
            e.preventDefault();

        });

        $('#ShiftUserssLeft').click(function (e) {
            $('#destinationUsers option:selected').appendTo('#sourceUsers');
            e.preventDefault();
        });

        $('#ShiftUsersRightAll').click(function (e) {
            $('#sourceUsers option').appendTo('#destinationUsers');
            e.preventDefault();
        });

        $('#ShiftUserssLeftAll').click(function (e) {
            $('#destinationUsers option').appendTo('#sourceUsers');
            e.preventDefault();
        });


    });
</script>

<h2>Subscription Details</h2>
<div>
    @using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new { name = "myForm1", id = "myForm1" }))
    {

        <input type="hidden" id="postedUsers" name="postedUsers" />
        <input type="hidden" id="postedExchanges" name="postedExchanges" />


        @Html.HiddenFor(m => m.SubscriptionTypeId, "SubscriptionTypeId");
        @Html.HiddenFor(m => m.CompanyId, "CompanyId");




        <div id="test"></div>

        <div class="Table">
            <div class="Row">
                <div class="borderlessCell" style="vertical-align:top">
                    <p>@Html.Label("Subscription Type")</p>
                </div>

                <div class="borderlessCell" style="vertical-align:top">
                    <p>
                        @Html.DisplayFor(m => m.SusbcriptionName, new { id = "lblSubscriptionType", name = "lblSubscriptionType" })
                    </p>
                </div>


            </div>

            <div class="Row">
                <div class="borderlessCell">

                </div>
                <div class="borderlessCell">
                    <p>@Html.Label("Available Exchanges")</p>
                </div>
                <div class="borderlessCell">

                </div>
                <div class="borderlessCell">
                    <p>@Html.Label("Selected Exchanges")</p>
                </div>
            </div>

            <div class="Row">
                <div class="borderlessCell" style="vertical-align:top">
                    <p>@Html.Label("List of Exchanges")</p>
                </div>
                <div class="borderlessCell" style="vertical-align:top">
                    @Html.ListBoxFor(m => m.AvailableExchangesIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.AvailableExchanges, "Value", "Text"), new { id = "sourceExchanges", name = "sourceExchanges", @style = "width:250px;height: 144px;", @CssClass = "MyListBox" })
                </div>


                <div class="borderlessCell">

                    <div class="Row">
                        <input type="submit" id="ShiftExchangesRight" value=">" style="vertical-align:top" />
                    </div>
                    <div class="Row">
                        <input type="submit" id="ShiftExchangesRightAll" value=">>" style="vertical-align:middle" />
                    </div>
                    <div class="Row">
                        <input type="submit" id="ShiftExchangesLeft" value="<" style="vertical-align:middle" />
                    </div>
                    <div class="Row">
                        <input type="submit" id="ShiftExchangesLeftAll" value="<<" style="vertical-align:bottom" />
                    </div>
                </div>



                <div id="selector" class="borderlessCell">
                    @Html.ListBoxFor(m => m.SelectedExchangesIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.SelectedExchanges, "Value", "Text"), new { id = "destinationExchanges", name = "destinationExchanges", @style = "width: 250px;height: 144px;", @CssClass = "MyListBox" })
                </div>

            </div>

            <div class="Row">
                <div class="borderlessCell">

                </div>
                <div class="borderlessCell">
                    <p>@Html.Label("Available Users")</p>
                </div>
                <div class="borderlessCell">

                </div>
                <div class="borderlessCell">
                    <p>@Html.Label("Suscribed Users")</p>
                </div>
            </div>

            <div class="Row">
                <div class="borderlessCell" style="vertical-align:top">
                    <p>@Html.Label("List of Recipients")</p>
                </div>
                <div class="borderlessCell" style="vertical-align:top">
                    @Html.ListBoxFor(m => m.AvailableUsersIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.AvailableUsers, "Value", "Text"), new { id = "sourceUsers", name = "sourceUsers", @style = "width:250px;height: 144px;", @CssClass = "MyListBox" })
                </div>

                <div class="borderlessCell" style="vertical-align:middle">
                    <p>
                        <input type="submit" id="ShiftUsersRight" value=">" />

                    </p>
                    <p>
                        <input type="submit" id="ShiftUsersRightAll" value=">>" />
                    </p>
                    <p>
                        <input type="submit" id="ShiftUsersLeft" value="<" />
                    </p>
                    <p>
                        <input type="submit" id="ShiftUsersLeftAll" value="<<" />
                    </p>
                </div>


                <div id="selector" class="borderlessCell">
                    @Html.ListBoxFor(m => m.SelectedUsersIds, new MultiSelectList((IEnumerable<SelectListItem>)Model.SelectedUsers, "Value", "Text"), new { id = "destinationUsers", name = "destinationUsers", @style = "width: 250px;height: 144px;", @CssClass = "MyListBox" })
                </div>

            </div>

            <div class="Row">

                <div class="borderlessCell">
                    <input type="submit" name="btn_Subscribe" value="Edit" />
                </div>

            </div>

        </div>
    }
</div>
我的视图模型的代码

 public class SubscriptionViewModel
    {

        public string PostedUsers { get; set; }
        public string PostedExchanges { get; set; }

        public int SubscriptionId { get; set; }
        public int SubscriptionTypeId { get; set; }
        public int CompanyId { get; set; }
        public string SusbcriptionName { get; set; }
        public string FullName { get; set; }
        public string UserSelection { get; set; }

        public IEnumerable<SelectListItem> AvailableExchanges { get; set; }
        public IEnumerable<SelectListItem> SelectedExchanges { get; set; }
        public IEnumerable<SelectListItem> AvailableUsers { get; set; }
        public IEnumerable<SelectListItem> SelectedUsers { get; set; }

        public IEnumerable<string> AvailableExchangesIds { get; set; }
        public IEnumerable<string> AvailableUsersIds { get; set; }
        public IEnumerable<string> SelectedExchangesIds { get; set; }
        public IEnumerable<string> SelectedUsersIds { get; set; }

    }

要解决这个问题,首先必须了解model binder是如何工作的。下面的帖子对此进行了更好的解释:

谢谢 罗摩克里希纳甘尼什酒店


…快乐编码

如果你学会了将它们标记为正确的话,也许你会得到答案!!展示你正在做的事情的点点滴滴并没有太大帮助。请更新您的问题,以显示您试图完成的任务的完整示例。为什么要为选定的Exchange SID创建隐藏输入?您已经为同一属性选择了一个名称相同的属性。模型绑定器将绑定到第一个而忽略第二个。您好@Tieson T。我已经粘贴了我的完整代码。@Stephen Muecke。我已经粘贴了我的完整代码。这个链接可能对每个人都有帮助,您也需要简要解释一下
[HttpPost]
        public ActionResult Edit(SubscriptionViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (BUREAUEntities bureauEntities = new BUREAUEntities())
                {
                    string[] stringSeparators = new string[] { "," };

                    if (model.SelectedUsersIds != null)
                    {
                        string[] postedUsersArray = model.SelectedUsersIds.ToArray();
                        string[] postedExchangesArray = model.SelectedExchangesIds.ToArray();

                        int[] postedUsers = Array.ConvertAll(postedUsersArray, int.Parse);
                        int[] postedExchanges = Array.ConvertAll(postedExchangesArray, int.Parse);

                        Avanade.Bureau.DataAccessLayer.DatabaseModel.Subscription a = new DataAccessLayer.DatabaseModel.Subscription
                        {
                            SubscriptionTypeId = model.SubscriptionTypeId,
                            IsScheduledNotification = false,
                            Active = true,
                            NotificationFrequencyInMinutes = 12,
                            Exchanges = GetExchanges(postedExchanges, bureauEntities),
                            Users = GetUsers(postedUsers, bureauEntities),
                            CompanyId = model.CompanyId

                        };
                        bureauEntities.Subscriptions.Add(a);
                        bureauEntities.SaveChanges();
                    }
                }
            }
            return RedirectToAction("Index", "Home");
        }
 public class SubscriptionViewModel
    {

        public string PostedUsers { get; set; }
        public string PostedExchanges { get; set; }

        public int SubscriptionId { get; set; }
        public int SubscriptionTypeId { get; set; }
        public int CompanyId { get; set; }
        public string SusbcriptionName { get; set; }
        public string FullName { get; set; }
        public string UserSelection { get; set; }

        public IEnumerable<SelectListItem> AvailableExchanges { get; set; }
        public IEnumerable<SelectListItem> SelectedExchanges { get; set; }
        public IEnumerable<SelectListItem> AvailableUsers { get; set; }
        public IEnumerable<SelectListItem> SelectedUsers { get; set; }

        public IEnumerable<string> AvailableExchangesIds { get; set; }
        public IEnumerable<string> AvailableUsersIds { get; set; }
        public IEnumerable<string> SelectedExchangesIds { get; set; }
        public IEnumerable<string> SelectedUsersIds { get; set; }

    }