English 中文(简体)
• 如何使多个下级名单的群体在MVC提出其价值 3
原标题:How to render multiple groups of drop down lists & submit their values in MVC 3

I have the following view model:

Public Class MyViewModel
    Public Property SelectedIDs As List(Of Integer)

    Public Property FilterListItems As SelectList
    Public Property SelectionListItems As SelectList
End Class

对于选定的独立工地财产中的每一户,我需要显示一份与FileterListItems财产中物品有关的清单的下降情况,以及ListItem公司所选择的选定独立工地价值需求的下降清单。 如果用户做出改变,则FileterListItems的下降将渗透到ListItem的下降中。 这将通过“ j”进行,因此,我需要两个滴名单的ids子,以便安装。 用户还能够在网页上添加另一组下级名单。 他们可以增加的下游名单数量没有限制。

When the user hits the "Save" button the selected item value in each drop down list that is populated with the SectionListItems needs to be sent back to the server and saved off.

什么是最佳办法可以做到这一点? 从我所看到的情况来看,我可以做的是每一处住宿和住宿;人工地使下岗名单减少,或者我可以提出部分观点,既包括下级名单。 我如何确保这些身份证不同,并且把这些身份证与格列拉连接起来,以便过滤器减少工作,正确的选定项目下降?

用户选择提交表格时如何回传给我的控制员? 我是否应该利用“金”来寻找所有退步,打造一个小目标,并且提出,或者说,与世界志愿人员联合会一起这样做?

用户如何在网页上增加另一套下降? 我看到,你可以通过酒吧使用衣物法,但清单需要重新载荷,需要更新。 如果我使用 j和某种模板,那么撒谎就是真的。 我也看到你可以使用Ajax。 行动链接,以提出另一种似乎较好的出路看法,但如何分配正确的身份证和住处;我如何把这些人连起来,看上去? 或许只是把它放在已准备好的文件的部分内容上?

最佳回答

I would probably adapt the view model a little bit:

public class MyViewModel
{
    public IEnumerable<ItemViewModel> Items { get; set; }
}

public class ItemViewModel
{
    public int SelectedFilterId { get; set; }
    public IEnumerable<SelectListItem> FilterListItems { get; set; }

    public int SelectedId { get; set; }
    public IEnumerable<SelectListItem> SelectionListItems { get; set; }
}

现在,我们的主要观点模式是,每个项目分别列出两个互换清单,用于过滤和甄选。 然后,我们有两个概念要在这里讨论:

  1. editing a variable length list
  2. cascading dropdown lists

The first point has been addressed by Steve Sanderson in this great post.

请谈第二点。 因此,我们将有4项行动:

  • GET Index => called when the page is initially loaded. It will query our DAL and fetch domain models that will be mapped to MyViewModel which we need to pass to the corresponding view
  • POST Index => called when the form is submitted in order to process the user input
  • GET Filter => passed a filter id and returning a JSON object containing a list of key/value pairs corresponding to the respective items of the SelectionListItems drop down list.
  • GET BlankEditorRow => returns a partial view representing a single group of drop down lists (see Steve Sanderson s blog post)

因此,请将此付诸行动:

public ActionResult Index()
{
    var filters = ... fetch from repo and map to view model
    var selectionItems = ... fetch from repo and map to view model

    var model = new MyViewModel
    {
        // TOOO: fetch from repo and map to view model
        Items = new[]
        {
            new ItemViewModel
            {
                SelectedFilterId = 1,
                FilterListItems = filters,
                SelectionListItems = selectionItems
            },
            new ItemViewModel
            {
                SelectedFilterId = 3,
                FilterListItems = filters,
                SelectionListItems = selectionItems
            },
        }
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // TODO: process the selected values here
    ...
}

public ActionResult Filter(int filterId)
{
    return Json(_repository.GetSelectionItems(filterId));
}

public ActionResult BlankEditorRow()
{
    var filters = ... fetch from repo and map to view model
    var selectionItems = ... fetch from repo and map to view model

    var model = new ItemViewModel
    {
        SelectedFilterId = 1,
        FilterListItems = filters,
        SelectionListItems = selectionItems
    };
    return PartialView("~/views/home/editortemplates/itemviewmodel.cshtml", model);
}

下一步是界定<代码>~/Views/Home/Index.cshtml>。 观点:

@model MyViewModel
@using (Html.BeginForm())
{
    <ul id="editorRows">
        @Html.EditorFor(x => x.Items)
    </ul>
    <button type="submit">OK</button>
}

@Html.ActionLink("Add another group of ddls", "BlankEditorRow", null, new { id = "addItem" })

以及相应的编辑模板(~/Views/Home/EditorTemplates/ItemViewModel.cshtml):

@model ItemViewModel
<li>
    <div>   
        @using (Html.BeginCollectionItem("items"))
        {
            @Html.DropDownListFor(
                x => x.SelectedFilterId,
                new SelectList(Model.FilterListItems, "Value", "Text", Model.SelectedFilterId),
                new { @class = "filter", data_filter_url = Url.Action("Filter") }
            )

            @Html.DropDownListFor(
                x => x.SelectedId,
                new SelectList(Model.SelectionListItems, "Value", "Text", Model.SelectedId),
                new { @class = "selection" }
            )
        }
    </div>
</li>

The last step is to wire up the cascading drop down list and the possibility to add groups dynamically using javascript. So in a separate js file:

var filterChange = function () {
    $( ul .filter ).unbind( change ).bind( change , function () {
        $.ajax({
            url: $(this).data( filter-url ),
            type:  GET ,
            cache: false,
            data: { filterId: $(this).val() },
            context: { selectionDdl: $(this).closest( li ).find( .selection ) },
            success: function (items) {
                var selectionDdl = this.selectionDdl;
                selectionDdl.empty();
                $.each(items, function (index, item) {
                    selectionDdl.append($( <option/> , {
                        value: item.ID,
                        text: item.Name
                    }));
                });
            }
        });
    });
};

$(function () {
    filterChange();

    $( #addItem ).click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $( #editorRows ).append(html);
                filterChange();
            }
        });
        return false;
    });
});
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签