【问题标题】:DropDownList - selectedValue without PostValue is not selectedDropDownList - 不选择没有 PostValue 的 selectedValue
【发布时间】:2011-03-06 19:25:49
【问题描述】:

我使用 asp.net mvc 2,但 DropDownListFor 助手有问题。 我的 ViewModel 包含一个 SelectList,其中包含所有必需的值和 1 个 SelectedValue。 当我在视图中使用 DropDownListFor 助手时,未选择 SelectedValue! 然后我选择另一个值并提交表单,在选定的 PostedValue 中的下一次渲染中。第一次渲染有什么问题?

<%=Html.LabelFor(m => m.Test)%>
<%=Html.DropDownListFor(m => m.Test, Model.TestList, new { tabindex = 1, @class = "selectbox" })%>
<%=Html.ValidationMessageFor(m => m.Test, null, new {@class = "text-hide", id = "Error-Test"})%>

【问题讨论】:

    标签: asp.net-mvc drop-down-menu


    【解决方案1】:

    根据您提供的内容,无法说明为什么它不起作用,因为您既没有显示控制器,也没有显示视图模型。

    这是一个有效的例子:

    型号:

    public class MyModel
    {
        public string Test { get; set; }
    
        public IEnumerable<SelectListItem> TestList
        {
            get
            {
                return new SelectList(new[] 
                {
                    new SelectListItem { Value = "1", Text = "text 1" },
                    new SelectListItem { Value = "2", Text = "text 2" },
                    new SelectListItem { Value = "3", Text = "text 3" },
                }, "Value", "Text", Test);
            }
        }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyModel { Test = "2" });
        }
    }
    

    查看:

    <%: Html.DropDownListFor(x => x.Test, Model.TestList) %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多