简体   繁体   English

MVC 5-MultiSelectList,使用模型“ SelectList”属性绑定值

[英]MVC 5 - MultiSelectList, Binding values with Model “SelectList” property

In my MVC project, I am having issue with binding MultiSelectLists with my Model properties. 在我的MVC项目中,我在将MultiSelectLists与Model属性绑定时遇到问题。 Although I can get the values through FormCollection object, but those are just the Selected IDs. 虽然我可以通过FormCollection对象获取值,但是这些仅仅是Selected ID。

Following is my Model Class which represents a Post. 以下是我代表帖子的Model类。

public class PostModel
{
    [Required(ErrorMessage = "Id is a required field")]
    public int Id { get; set; }

    [Required(ErrorMessage = "Post Title is required")]
    [StringLength(500)]
    public string Title { get; set; }

    .......

    public SelectList UnassignedTags { get; set; }
    public SelectList AssignedTags { get; set; }
}

Please note that this is just a Model class and has two additional properties (UnassignedTags and AssignedTags) only for the purpose of displaying them on screen and for keeping the values bound between calls. 请注意,这只是一个Model类,并且具有两个附加属性(UnassignedTags和AssignedTags)仅用于在屏幕上显示它们并保持调用之间的值绑定。

A Single Post can be marked with multiple Tags. 单个帖子可以标记多个标签。 On my View, I want two multi select Lists. 在我的视图上,我想要两个多选列表。 One to hold Unassigned Tags (Text and Value) and other to Hold Assigned Tags (Text and Value). 一个用于保存未分配的标签(文本和值),另一个用于保存分配的标签(文本和值)。

On My view, I show a Multi Select list for these lists using following code. 在我的视图上,我使用以下代码显示这些列表的“多选”列表。

@Html.ListBox("UnassignedTagss", Model.UnassignedTags, new { @class = "form-control" })

@Html.ListBox("AssignedTagss", Model.AssignedTags, new { @class = "form-control" })

Everything is ok when I load the record on screen. 将记录加载到屏幕上时,一切正常。

Once loaded, I let user move items between lists through javascript. 加载后,我让用户通过javascript在列表之间移动项目。

The problem comes when Form gets posted back for editions. 问题是当Form被发回版本时。 I am unable to bind Listbox values with my model properties. 我无法将列表框值与模型属性绑定。

Following are my Action Methods for Showing / Editing Post record. 以下是我显示/编辑帖子记录的操作方法。

public ActionResult PostDtls(int postId)
{
    …..
}

[HttpPost]
public ActionResult PostDtls(PostModel post, FormCollection collection)
{
    …….
}

Is there any way I can bind ListBox items (regardless of selected or not) to my Model SelectList properties ie UnAssignedTags and AssignedTags ? 有什么方法可以将ListBox项(无论是否选中)绑定到我的Model SelectList属性,即UnAssignedTags和AssignedTags?

At the moment, I am selecting all items of ListBox through JQuery before posting the form back to the server. 目前,我正在通过JQuery选择ListBox的所有项目,然后将表单发布回服务器。 This way, I get comma separated Ids in FormCollection Object. 这样,我在FormCollection对象中得到逗号分隔的ID。 However, the problem is that if I have to redisplay the view to the user because of an Invalid ModelState, I have to fetch the Tags from the database based on the Ids available in FormCollection object. 但是,问题在于,如果由于无效的ModelState而必须将视图重新显示给用户,则必须基于FormCollection对象中可用的ID从数据库中获取标签。 I know the above solution is bad. 我知道上述解决方案是不好的。 And I need to know a good approach to implement this requirement. 而且我需要知道一种实现此要求的好方法。

I'm not sure why you are taking this approach. 我不确定您为什么要采用这种方法。 The first thing you need are 2 additional properties in your view model (I'm assuming the value type is int ) 您需要做的第一件事是在视图模型中添加2个其他属性(我假设值类型为int

public int[] SelectedUnassignedTags { get; set; }
public int[] SelectedAssignedTags  { get; set; }

Then create the DropDownLists as follows 然后如下创建DropDownLists

@Html.ListBoxFor(m => m.SelectedUnassignedTags, Model.UnassignedTags, ...)
@Html.ListBoxFor(m => m.SelectedUnassignedTags, Model.UnassignedTags, ...)

On postback, SelectedUnassignedTags and SelectedAssignedTags will contain the values that have been selected in the respective listboxes, so for example if you selected these 2 options 在回发时, SelectedUnassignedTagsSelectedAssignedTags将包含在相应列表框中SelectedAssignedTags的值,例如,如果您选择了这两个选项

<option value="2">Item No 2</option>
<option value="4">Item No 4</option>

then SelectedUnassignedTags would be an array containing 2 and 4 那么SelectedUnassignedTags将是一个包含2和4的数组

In you post method (don't see any need for FormCollection ) 在您发布方法中(不需要任何FormCollection

public ActionResult PostDtls(PostModel post)
{
  if (!ModelState.IsValid())
  {
    // Repopulate you select lists (as you would have done in the get method)
    return View(post);
  }
}

To try and construct all the necessary option values, associated text and selected status in javascript and post back that much additional data, and then reconstruct them in the post method is likely to be more expensive than the database call (and of course, you could always cache them to avoid calling the database) 要尝试在javascript中构造所有必要的选项值,关联的文本和所选状态,然后回发大量其他数据,然后在post方法中重建它们,可能比数据库调用更昂贵(当然,您可以总是缓存它们以避免调用数据库)

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

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