1

我有以下问题。在我的视图模型中,我定义了一些列表属性,如下所示:

public class BasketAndOrderSearchCriteriaViewModel
{
    List<KeyValuePair> currencies;
    public ICollection<KeyValuePair> Currencies
    {
        get
        {
            if (this.currencies == null)
                this.currencies = new List<KeyValuePair>();
            return this.currencies;
        }
    }

    List<KeyValuePair> deliverMethods;
    public ICollection<KeyValuePair> DeliveryMethods
    {
        get
        {
            if (this.deliverMethods == null)
                this.deliverMethods = new List<KeyValuePair>();
            return this.deliverMethods;
        }
    }
 }

此视图模型嵌入在另一个视图模型中:

 public class BasketAndOrderSearchViewModel
 {
    public BasketAndOrderSearchCriteriaViewModel Criteria
    {
        [System.Diagnostics.DebuggerStepThrough]
        get { return this.criteria; }
    }
 }

我使用 2 种操作方法;一个用于 GET,另一个用于 POST:

[HttpGet]
public ActionResult Search(BasketAndOrderSearchViewModel model){...}

[HttpPost]
public ActionResult SubmitSearch(BasketAndOrderSearchViewModel model){...}

在视图中,我使用 EditorFor-Html Helper 实现了整个视图模型,它不想自动显示 List 属性的 DropDownLists! 1.问:如何让EditorFor显示DropDownLists?

由于我不知道如何使用 EditorFor 显示 DropDownLists,因此我使用了 DropDownList Html 帮助器并通过视图模型填充它,如下所示:

    public IEnumerable<SelectListItem> DeliveryMethodAsSelectListItem()
    {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem()
        {
            Selected = true,
            Text = "<Choose Delivery method>",
            Value = "0"
        });
        foreach (var item in this.DeliveryMethods)
        {
            list.Add(new SelectListItem()
            {
                Selected = false,
                Text = item.Value,
                Value = item.Key
            });
        }

        return list;
    }

我的 2. 问题:如您所见,我将视图模型传递给具有 POST 属性的操作方法!有没有办法让 DropDownList 的选定值绑定到传递的视图模型?目前所有 DropDownList 都是空的,并且只能通过 Request.Form 获取所选值,我绝对想避免!

我将不胜感激有关此的一些想法或提示!

4

2 回答 2

2

对于像我这样的人,我建议你从http://www.asp.net/mvc/tutorials/mvc-music-store-part-1完全下载教程,其中涵盖了这个和大多数与 .NET MVC 应用程序相关的常用技术。

无论如何,你的帖子和答案真的很有用(如果我能投票给你,我会:)

于 2010-10-31T19:57:07.093 回答
1

让我们尝试解决这个问题:

对问题 1 的回答:如何让 EditorFor 显示 DropDownLists?

当您调用时,Html.EditorFor()您可以将额外的 ViewData 值传递给 EdiorTemplate 视图:

<%: Html.EditorFor(model => Model.Criteria, new { DeliveryMethods = Model.DeliveryMethods, Currencies = Model.Currencies}) %>

现在您已在 EditorTemplate 中初始化并可用ViewData["DeliveryMethods"]ViewData["Currencies"]

在您的EditorTemplate情况下,您需要以某种方式调用并将这些条目转换为 DropDowns / SelectLists。假设您有一个 ascx 类型的文件,System.Web.Mvc.ViewUserControl<BasketAndOrderSearchCriteriaViewModel>您可以执行以下操作:

<%: Html.LabelFor(model => model.DeliveryMethods) %>
<%: Html.DropDownList("SelectedDeliveryMethod", new SelectList(ViewData["DeliveryMethods"] as IEnumerable, "SelectedDeliveryMethod", "Key", "value", Model.SelectedDeliveryMethod)) %>

货币也是如此。

<%: Html.LabelFor(model => model.Currencies) %>
<%: Html.DropDownList("SelectedCurrency", new SelectList(ViewData["Currencies"] as IEnumerable, "SelectedDeliveryMethod", "Key", "value", Model.SelectedCurrency)) %>

此设置将使您DeliveryMethodAsSelectListItem()过时,您可以使用任何类型的列表。意味着您不受 KeyValuePairs 的约束。Html.DropDownList()从现在开始,您只需要调整您的通话即可。

如您所见,我为您的 引入了一些新属性BasketAndOrderSearchCriteriaViewModel

Model.SelectedDeliveryMethod
Model.SelectedCurrency

它们用于存储当前选定的值。

对问题 2 的回答:有没有办法让 DropDownList 的选定值绑定到传递的视图模型?

在 EditorFor 模板中,我们将新创建的属性Model.SelectedDeliveryMethodModel.SelectedCurrency属性作为SelectedValue Parameter(参见DropDownList 扩展方法的第 4 次重载)。

现在我们已经让 View 完成了它的工作:我们如何在POST操作中获取当前选择的值?

现在这真的很容易:

[HttpPost]
public ActionResult SubmitSearch(BasketAndOrderSearchViewModel model)
{
    ...
    var selectedDeliveryMethod = model.Criteria.SelectedDeliveryMethod;
    var selectedCurrency model.Criteria.SelectedDeliveryMethod;
    ...
}

注意:我现在没有 IDE 来测试它,但它应该可以解决问题,或者至少可以告诉你该往哪个方向发展。

于 2010-06-11T09:06:08.033 回答