ASP.NET MVC:jQuery UI 可选表单发布和验证

发布于 2024-12-14 16:11:42 字数 1563 浏览 2 评论 0原文

我有一个向用户显示的项目列表,并允许他们通过利用 jQuery UI Selectable 交互来选择其中的一个或多个。为了将所选值发布回控制器,我在每个可选项目中包含一个隐藏的输入字段,我通过所选事件的 javascript 设置该输入字段。这是我如何设置

Model

public class ItemsViewModel
{
     public List<Item> Items { get; set; }
}

public class Item 
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool Selected { get; set; }
}

Html

<ul id="Items">
     <li>
          <label>Item 1</label>
          <input type="hidden" id="Items[0]_Id" name="Id" value="1" />
          <input type="hidden" id="Items[0]_Selected" name="Items[0].Selected" class="is-selected" value="False" />
     </li>
     <li>
          <label>Item 2</label>
          <input type="hidden" id="Items[1]_Id" name="Items[1].Id" value="2" />
          <input type="hidden" id="Items[1]_Selected" name="Items[1].Selected" class="is-selected" value="False" />
     </li>
</ul>

JavaScript

 $('#Items').selectable({
        filter: 'li',
        selected: function (event, ui) {
            $(ui.selected).find('input.is-selected').val('True');
        },
        unselected: function (event, ui) {
            $(ui.unselected).find('input.is-selected').val('False');
        }
    });

我的第一个问题,虽然这有效,但它不是最漂亮的解决方案。有谁有更好的方法来发布可选项目?

其次,我需要确保用户至少从列表中选择了一项。是否可以连接 jQuery 客户端验证以确保用户至少选择了一项?我确实对实现这一点的方法有一些想法,并且创建自定义验证属性没有问题,但我想在我把一些东西放在一起之前我会问它是否已经完成或者最好的方法。

谢谢

I have a list of items that I display to a user and allow them to select one or more of them by utilizing the jQuery UI Selectable interaction. In order to post the selected values back to the Controller I am including a hidden input field within each selectable item that I am setting via javascript on the selected event. Here is an example of how I have it setup

Model

public class ItemsViewModel
{
     public List<Item> Items { get; set; }
}

public class Item 
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool Selected { get; set; }
}

Html

<ul id="Items">
     <li>
          <label>Item 1</label>
          <input type="hidden" id="Items[0]_Id" name="Id" value="1" />
          <input type="hidden" id="Items[0]_Selected" name="Items[0].Selected" class="is-selected" value="False" />
     </li>
     <li>
          <label>Item 2</label>
          <input type="hidden" id="Items[1]_Id" name="Items[1].Id" value="2" />
          <input type="hidden" id="Items[1]_Selected" name="Items[1].Selected" class="is-selected" value="False" />
     </li>
</ul>

JavaScript

 $('#Items').selectable({
        filter: 'li',
        selected: function (event, ui) {
            $(ui.selected).find('input.is-selected').val('True');
        },
        unselected: function (event, ui) {
            $(ui.unselected).find('input.is-selected').val('False');
        }
    });

My first question, although this works, it isn't the prettiest solution. Does anyone have any better ways of posting the selectable items?

Second, I need to ensure that the user has selected at least one item from the list. Is it possible to hook up jQuery client side validation to ensure that the user has selected at least one item? I do have some thoughts about ways to implement this and I have no problem creating a custom validation attribute but I thought I would ask if it has already been done or the best way to of doing it before I threw something together.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2024-12-21 16:11:45

您尚未显示用于发布项目的代码。对我来说,这似乎是有趣的一点。

至于依赖隐藏字段,我不知道你为什么要这样做。为什么不在所选事件中填充一个项目数组,然后在执行 AJAX POST 时发送该数组?

itemsToPost = [];

$('#Items').selectable({ 
    filter: 'li', 
    selected: function (event, ui) {
        itemsToPost.push( {id: ui.attr('id'), name:ui.text()}); 
    }, 
    ...

$.ajax({url     : "/whatever/ASPNETMVC/Endpoint",
        cache   : false,
        type    : "POST", // http method
        dataType: "json",
        data    : itemsToPost,
        error   : function(xhr,status,error){
                ...
        },
        success : function(msg, arg2, xhr){
                ...
        });

Youu haven't shown the code you use to post the items. That seems to be the interesting bit, to me.

As for relying on hidden fields, I don't know why you'd do that. Why not just populate an array of items in the selected event, then send that array when doing the AJAX POST?

itemsToPost = [];

$('#Items').selectable({ 
    filter: 'li', 
    selected: function (event, ui) {
        itemsToPost.push( {id: ui.attr('id'), name:ui.text()}); 
    }, 
    ...

$.ajax({url     : "/whatever/ASPNETMVC/Endpoint",
        cache   : false,
        type    : "POST", // http method
        dataType: "json",
        data    : itemsToPost,
        error   : function(xhr,status,error){
                ...
        },
        success : function(msg, arg2, xhr){
                ...
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文