使用下拉型窗体绑定到一个列表。结合对失败下拉1路 [英] Model binding form to a List using dropdown. binding failing 1 way for dropdown

查看:86
本文介绍了使用下拉型窗体绑定到一个列表。结合对失败下拉1路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Sessies的操作。在这次行动中,我创建从表Sessies'的对象。如果不存在我添加他们在DB中,如果已经有Sessies连接到'Reeksen'对象物体它们,我加载Sessies入形式,以便它们可以被编辑。所以我有一个创建和编辑在1和相同的形式。

I have an action called Sessies. In this action i am creating 'Sessies' objects from a form. if they don't exist i add them in the DB, if there are already Sessies objects connected to the 'Reeksen' object, i load the 'Sessies' into the form so that they can be edited. so i have a create and edit in 1 and the same form.

此外,'Reeksen'具有不能改变'Sessies'的一个predefined号码。所以我让用户使所有Sessies'在一个时间(COS sessies量将是从1到10)

Also, a 'Reeksen' has a predefined number of 'Sessies' which can not be changed. so i let the user make all 'Sessies' in one time (cos the amount of sessies will be from 1 to 10)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<MVC2_NASTEST.Models.FlatSessie>>" %>
...
<h2>
    Sessies</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <% for (int i = 0; i < Model.Count; i++) { %>

    <%= Html.HiddenFor(model => model[i].Ses_ID)%>
    <%= Html.HiddenFor(model => model[i].Ses_Rks_ID)%>

    <div class="editor-label">
        <%= Html.LabelFor(model => model[i].Ses_Nummer)%>
    </div>
    <div class="editor-field">
        <%= Html.HiddenFor(model => model[i].Ses_Nummer)%>
        <%= Html.Label(Model[i].Ses_Nummer.ToString())%>
    </div>

    ....

    <div class="editor-label">
        <%= Html.LabelFor(model => model[i].Ses_LG_ID)%>
    </div>
    <div class="editor-field">
        <%= Html.DropDownListFor(model => model[i].Ses_LG_ID, MVC2_NASTEST.MvcApplication.lesgeverList(), "Selecteer een lesgever...")%>
        <%= Html.ValidationMessageFor(model => model[i].Ses_LG_ID)%>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model[i].Ses_Lpl_ID)%>
    </div>
    <div class="editor-field">
        <%= Html.DropDownListFor(model => model[i].Ses_Lpl_ID, (ViewData["lesplist"] as List<List<SelectListItem>>)[i], "Selecteer een lesplaats...")%>
        <%= Html.ValidationMessageFor(model => model[i].Ses_Lpl_ID)%>
    </div>
    <% } %>
    <p>
        <input type="submit" value="Create" />
    </p>

在我的aspx我使用for循环而越过列表(FlatSessie是Sessie扁平的字符串。)

in my aspx i use a for loop which goes over the List (a FlatSessie is a Sessie flattened strings.)

  namespace MVC2_NASTEST.Models {

    public partial class FlatSessie {
        public int Ses_ID { get; set; }
        public int Ses_Nummer { get; set; }
        public string Ses_Datum { get; set; }
        public string Ses_Beginuur { get; set; }
        public string Ses_Einduur { get; set; }
        public int Ses_Lpl_ID { get; set; }
        public int Ses_Rks_ID { get; set; }
        public int Ses_LG_ID { get; set; }
    }
  }

所以,在我的code那么它是这样的:

so, in my code it goes like this:

                    int antses = m.Mod_AantalSessies.Value;

                    List<List<SelectListItem>> lpllst = new List<List<SelectListItem>>(antses);

                    List<FlatSessie> sl = new List<FlatSessie>(antses);

                    Reeksen rks = _db.Reeksens.First(r => r.Rks_ID == id)

...

List<Sessies> sesl = rks.Sessies.ToList();

                        for (int i = 0; i < antses; i++) {
                            sl.Add(Mapper.Map<Sessies, FlatSessie>(sesl[i]));
                            lpllst.Add(MvcApplication.lesplaatsList(schooljaarparam, sesl[i].Ses_Lpl_ID));
                        }

...

    ViewData["lesplist"] = lpllst;
    ViewData["lglist"] = MvcApplication.lesgeverList();
    return View(sl);

和lesgeverlist()方法

and the lesgeverlist() method

public static List<SelectListItem> lesgeverList() {
            NASDataContext _db = new NASDataContext();
            var lesg = (from l in _db.Lesgevers
                        where l.LG_Naam != "leeg"
                        orderby l.LG_Naam
                        select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => m.LG_ID < -1);
            return lesg.ToList();
        }

现在的问题:

这一切工作出色。这样的例子不胜枚举的ASPX,我得到的形式尽可能多倍有列表中的项目,并回传作品也,解析去和一切。所以一切除了1点好:在下拉菜单

this all works brilliantly. the List goes to the ASPX, i get the form as much times as there are items in the List, and postback works also, the parsing goes and everything. so all is good except for 1 point: the dropdowns.

通常MVC我不为的SelectList或列表设置的任何选择的价值,因为它们不需要它,在编辑页面,MVC设定有约束力的那些选定的项目本身。

usually in MVC i don't set any selected value for a SelectList or for a List because they dont need it, in the Edit page, MVC sets those selected items itself on binding.

现在然而,在foreach循环的形式,各个领域得到填补的除了的下拉框,这些没有收到他们的初始值。

now however, with the form in the Foreach loop, all fields get filled besides the dropdown boxes, these do not receive their 'initial value'.

然而,当选择为我设置列表中的项目,它得到的在表单中选择。 (从计算机[lesplist]看到),但是当我送一个正常的列表没有选定值,模型绑定不传播它给该字段值下拉的SelectedValue的。

however when i set an item in the List as selected, it does get selected in the form. (as seen from the ViewData["lesplist"]) but when i send a normal List with no selected value, the model binder does not propagate it's given value for that field to the selectedvalue of the dropdown.

然而,当我做一个表单提交,我返回的观点(因为验证失败)下拉式菜单中保留他们的价值。

however, when i do a form submit, and i return the view (because of validation failed) the dropdowns DO keep their value.

这是可以解决的,或者这只是在MVC2缺陷?

Is this fixable, or is this just a flaw in MVC2?

推荐答案

<一个href=\"http://stackoverflow.com/questions/3501498/dropdownlistfor-not-binding-on-edit-view-with-repeating-items-listt\">http://stackoverflow.com/questions/3501498/dropdownlistfor-not-binding-on-edit-view-with-repeating-items-listt

就有答案了:)

这篇关于使用下拉型窗体绑定到一个列表。结合对失败下拉1路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆