简体   繁体   English

将ListBoxFor(多个)绑定回Model

[英]Bind ListBoxFor (Multiple) back to Model

I have a MVC view that renders what basically equates to a KeyValuePair using the Razor syntax below, and then generating the following HTML. 我有一个MVC视图,该视图使用下面的Razor语法渲染基本上等同于KeyValuePair的内容,然后生成以下HTML。

@Html.DropDownListFor(x => x.SelectedItems, new SelectList(Model.SelectedItems, "Key", "Key"), new { Class = "selectList selectedList", size = "2" })

HTML: HTML:

<select class="selectList selectedList" id="SelectedItems" name="SelectedItems" size="2">
   <option value="842">Item 1</option>
   <option value="326">Item 2</option>
   <option value="327">Item 3</option>
</select>

I am posting the form manually using Jquery and generic function to POST our forms, as below: 我正在使用Jquery和通用函数手动过帐表单以过帐我们的表单,如下所示:

function GenericSubmit(formSelector, sender, callback) {
    if (typeof (sender) != "undefined" && $(sender).hasClass('disabled')) {
        return false;
    }

    var $that = $(formSelector);
    var that = $that.get(0);
    if ($that.valid()) {
        $.ajax({
            url: that.action,
            type: that.method,
            data: $(that).serialize(),
            success: function (data, textStatus, jqXHR) {
                callback.call(that, data);
            }
        });
    }
    return false;
}

However the issue I am experiencing, is that only data that is being sent is the actual values (I expect this is how JQ works..), but I need to bind to an IEnumerable. 但是我遇到的问题是,只有要发送的数据才是实际值(我希望这就是JQ的工作方式..),但是我需要绑定到IEnumerable。

From looking at the POST data that is sent to the form, I can only see the following values being sent - which I would expect why my Model has a null collection. 通过查看发送到表单的POST数据,我只能看到正在发送以下值-我可以预期为什么我的模型具有null集合。

SelectedItems:842
SelectedItems:326
SelectedItems:327

My Model is as follows: 我的模型如下:

/// <summary>
/// An response for dealing with list type entities
/// </summary>
public class ListEntityResponse : EntityScreenResponse
{
    /// <summary>
    /// Contains a Enumerable of items that can be selected
    /// </summary>
    public List<KeyValueViewModel> AvailableItems { get; set; }

    /// <summary>
    /// Contains a Enumerable of items that have been selected
    /// </summary>
    public List<KeyValueViewModel> SelectedItems { get; set; }

    public ListEntityResponse()
    {
        AvailableItems = new List<KeyValueViewModel>();

        SelectedItems = new List<KeyValueViewModel>();
    }


}

For added clarity - here is my KeyValueViewModel: 为了更加清晰-这是我的KeyValueViewModel:

 public class KeyValueViewModel
    {
        public string Key { get; set; }

        public string Value { get; set; }
    }

当前结果

I've searched high and low for this, but can't seem to find anything on the subject that works, any help would be appreciated! 我为此进行了高低搜寻,但似乎找不到任何可行的主题,我们将不胜感激!

Thanks, 谢谢,

//Just realized I had misread your question. //刚刚意识到我读错了你的问题。

If you want yo bind to multiple values you need to use ListBoxFor 如果要绑定到多个值,则需要使用ListBoxFor

// //

So why do you need the controller to receive all this info from the view if its something that you probably already have in your backend? 那么,为什么您的控制器可能需要从视图中接收所有这些信息(如果后端中可能已经包含了某些信息)呢?

It makes sense for it to only care about the things you submitted in the form. 只关心您在表单中提交的内容是有意义的。

If what you need is to repopulate this data to render the view again (because there were validation errors, here's an elegant approach) 如果您需要重新填充此数据以再次呈现视图(因为存在验证错误,这是一种优雅的方法)

http://www.paulstovell.com/clean-aspnet-mvc-controllers http://www.paulstovell.com/clean-aspnet-mvc-controllers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM