微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Razor Pages 中的帖子上绑定多个列表项

如何解决在 Razor Pages 中的帖子上绑定多个列表项

我想绑定一个在帖子上有多个列表项的表单,下面是我的代码

   <form asp-page-handler="InsertAssignedStates" method="post">
           name="assignedStates" />
                <div class="row">
                    <div class="dual-list list-left col-md-5">
                        <div class="well text-right">
                            <select asp-for="Stest"
                                    multiple="multiple"
                                    size="10"
                                    class="form-control mb-2 mr-sm-2 ml-sm-3 leftList"
                                    id="assignedStateList"
                                    asp-items="@Model.AssignedTaxStateList">
                            </select>
                        </div>
                    </div>

我在页面模型中有这个:

 [BindProperty(SupportsGet = true)]
 public IList<Tax> AssignedTaxStates { get; set; }
 public IActionResult OnPostInsertAssignedStates()
    {
  
     return Redirect("Index/");
    }

它命中了断点但没有被绑定。

解决方法

Asp.net core 将数据与 name 属性绑定。这里是一个演示,在剃刀页面中将输入与列表绑定,并将多个选择与列表绑定。

型号:

public class Tax
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

cshtml:

<form asp-page-handler="InsertAssignedStates" method="post">
    <div class="row">
        <div class="dual-list list-left col-md-5">
            <div class="well text-right">
                <select asp-for="Stest"
                        multiple="multiple"
                        size="10"
                        class="form-control mb-2 mr-sm-2 ml-sm-3 leftList"
                        id="assignedStateList"
                        asp-items="@Model.AssignedTaxStateList">
                </select>
            </div>
            @{ var i = 0;}
            @foreach (var item in Model.AssignedTaxStates)
            { 
                <div class="well text-right">
                    <input value="@item.Id"  name="AssignedTaxStates[@i].Id"/>
                    <input value="@item.Name" name="AssignedTaxStates[@i].Name"/>

                </div>
                i++;
            }
        </div>
    </div>
    <input type="submit" value="submit" />
</form>

cshtml.cs:

public class TestModel : PageModel
    {
        [BindProperty]
        public List<SelectListItem> AssignedTaxStateList { get; set; }
        [BindProperty]
        public IList<Tax> AssignedTaxStates { get; set; }
        [BindProperty]
        public List<string> Stest { get; set; }
        public void OnGet()
        {
            AssignedTaxStateList = new List<SelectListItem> { new SelectListItem { Text = "1",Value = "1" },new SelectListItem { Text = "2",Value = "2" } };
            AssignedTaxStates = new List<Tax> { new Tax { Id = 1,Name = "tax1" },new Tax { Id = 2,Name = "tax2" },new Tax { Id = 3,Name = "tax3" } };
        }

        public IActionResult OnPostInsertAssignedStates()
        {

            return Redirect("Index/");
        }
    }

结果: enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。