DataBinding ASP.NET MVC 2中的DropDownList

时间:2010-04-15 19:38:33

标签: c# asp.net asp.net-mvc

经过几年的网络开发后,我非常沮丧地试图转向MVC。

这是我无法解决的极其简单的问题: 我有一个名为StateProvince的表中的状态列表。 我有一个DropDownList。 我希望DropDownList显示所有状态。

保持简单,我对MVC一无所知。

这就是我所拥有的。所有这些给了我一个DropDownList填充“System.Web.Mvc.SelectListItem”。

行动:

  public ActionResult Create()
    {
        var dbTimecard = new TimecardDbDataContext();
        IEnumerable<SelectListItem> stateProvinces = dbTimecard.StateProvinces.Select(p => new SelectListItem
        {
            Value = p.StateProvinceId.ToString(),
            Text = p.Name
        });
        SelectList theList = new SelectList(stateProvinces);
        ViewData["StateProvince"] = theList;
        return View();
    } 

查看:

<div class="editor-label">
                <%: Html.LabelFor(model => model.StateProvinceId) %>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.StateProvinceId, (SelectList)ViewData["StateProvince"])%>
                <%: Html.ValidationMessageFor(model => model.StateProvinceId) %>
            </div>

4 个答案:

答案 0 :(得分:1)

以下是我要找的内容:

动作:

public ActionResult Create()
    {
        var dbTimecard = new TimecardDbDataContext();
        IEnumerable<SelectListItem> stateProvinces = dbTimecard.StateProvinces.Select(p => new SelectListItem
        {
            Value = p.StateProvinceId.ToString(),
            Text = p.Name
        });
        ViewData["StateProvince"] = stateProvinces;
        return View();
    } 

查看:

<div class="editor-field">
                <%: Html.DropDownListFor(model => model.StateProvinceId, (IEnumerable<SelectListItem>)ViewData["StateProvince"])%>
                <%: Html.ValidationMessageFor(model => model.StateProvinceId) %>
            </div>

答案 1 :(得分:1)

尝试替换此

SelectList theList = new SelectList(stateProvinces);

有了......

SelectList theList = new SelectList(stateProvinces, "Value", "Text");

答案 2 :(得分:0)

常见浏览器不支持HTML表单中的PUT动词。您可能需要使用ajax处理此问题:

$(function() {
    // when some button is clicked:
    $('#someButton').click(function() {
        $.ajax({
            url: '/controller/action',
            data: { selectedState: $('#StateProvinceId').val() },
            type: 'PUT',
            success: function(data) {
                alert('state successfully submitted');
            }
        });
        return false;
    });
});

答案 3 :(得分:0)

发布数据时,DropDown中列出的项目不会回发到模型中,因此我假设您没有再次获取它们并将其重新添加到模型中,然后再将模型返回到模型中观点。

您需要在帖子上确认您正在填充Model.StateProvinces,然后将其传递给您的视图。与WebForms不同,只保留值,这将保留DropDownList中的ViewState项并为您重建它们。

所以假设您的控制器看起来像:

// POST: /YourController/YourAction/{ID}
[HttpPost]
public ActionResult YourAction(YourModel model, int id)
{
    // model.StateProvinceId has the selected value

    // you need to fill StateProvinces since it is empty as the list is not posted
    // back so it is not autofilled into your model.
    model.StateProvinces = LoadYourStateProvincesList();

    // This would blow up if StateProvinces is null because your View is assuming that
    // it has a valid list of StateProvinces in it to build the DropDown
    return View(model);
}

我实际上回答了一段时间的问题,这可能有助于解释如何处理DropDown列表:

Best way of implementing DropDownList in ASP.NET MVC 2?

编辑:根据您的评论,您甚至没有获得第一个列表...好吧,您可能没有正确设置模型。你有模特吗?您的创建只返回View(),其中没有模型数据。您需要创建一个模型并将其返回。

例如

public class YourModel
{
    public int StateProvinceId { get; set; }
    /// ... whatever else you need
}

// Then in your view your controller you need to set it and create it
View(new YourModel() { StateProvinceId = 123 };);

你需要这个模型,这样当表单被回发时,你可以检索这个值,因为MVC会将值填充到你的模型中,就像我上面发布的关于POST部分的例子一样。

编辑:好了,现在这个问题比原来的问题更清晰,更容易解决问题。问题是你的SelectList需要告诉它哪些字段放在哪里:

SelectList theList = new SelectList(stateProvinces, "Value", "Text");

在原来的问题中你有:

<%: Html.DropDownListFor(model => model.StateProvinceId, new SelectList(Model.StateProvinces, "StateProvinceId", "Name") )%>

这是完全正确的但是你是如何构建错误的Model.StateProvinces所以它让我不再认为它必须是别的东西。

相关问题