2012-05-04 36 views
2

我有一个MVC视图,呈现什么基本相当于一个KeyValuePair使用下面的Razor语法,然后生成下面的HTML。绑定ListBoxFor(多)返回到模型

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

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> 

我张贴手动使用jQuery和通用函数为POST我们的形式,下面的表格:

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; 
} 

但是我遇到的问题,是只有正在发送的数据是实际值(我期望这是JQ的工作原理..),但我需要绑定到IEnumerable。

从查看发送到表单的POST数据,我只能看到下面的值被发送 - 我期望为什么我的模型有一个空集合。

SelectedItems:842 
SelectedItems:326 
SelectedItems:327 

我的模型如下:

/// <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>(); 
    } 


} 

为了更加清楚 - 这是我KeyValueViewModel:

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

     public string Value { get; set; } 
    } 

Current Results

我已经搜查高和低本,但似乎无法找到任何有用的主题,任何帮助将不胜感激!

谢谢,

回答

0

//只是意识到我误解了你的问题。

如果你想哟结合到你需要使用ListBoxFor

//

那么,为什么你需要的控制器接收来自视图中的所有这些信息多个值,如果它的东西,你可能已经在你的后端?

它是有道理的,它只关心你在表单中提交的东西。

如果您需要的是填充此数据再次渲染视图(因为有验证错误,这里是一个优雅的方法)

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

+0

是的,我会在我的后台,但我不真的不想再次击中数据库,因为这个数据..但我同意它应该只关心以形式提交的东西......但另一方面,我确实需要它作为IEnum,因为它将被用于用于渲染项目以便它们可以编辑选择。 感谢您的链接 - 我会给它一个阅读,看看查克诺里斯说什么! –

+0

仍然没有快乐我害怕,我添加了一张图片来帮助说明我的问题 –