Html.DropdownListFor 未设置选定值 [英] Html.DropdownListFor selected value not being set

查看:24
本文介绍了Html.DropdownListFor 未设置选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 Html.DropDownListFor 的选定值?我一直在网上看,看到它可以通过使用第四个参数来实现,如下所示:

How can I set the selected value of a Html.DropDownListFor? I've been having a look online and have seen that it can be achieved by using the fourth parameter so like the below:

@Html.DropDownListFor(m => m, new SelectList(Model, "Code", "Name", 0),  "Please select a country")

我的选择列表显示如下:

My select list then display like this:

<select id="ShipFromCountries" name="ShipFromCountries">
     <option value="">Please select a country</option>
     <option value="GB">United Kingdom</option>
     <option value="US">United States</option>
     ...
</select>

但出于某种原因,英国仍然被选中,但我希望选择请选择一个国家".

But for some reason United Kingdom remains selected but I want "Please select a country" to be selected.

有人知道我是如何做到这一点的吗?

Anyone know how I can achieve this?

我已经更新了我的代码,因为功能略有变化,但似乎仍然遇到了这个问题.这是我的看法:

I've updated my code as there was a slight change in functionality however still seem to be encountering this problem. This is what is in my view:

@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

1 是我想要选择的 option 的 ID,我也尝试过使用 option 的文本,但这也可以不工作.

1 is the Id of the option that I want selected, I have also tried with the text of the option but that also does not work.

有什么想法吗?

推荐答案

你的代码有一些概念上的问题:

Your code has some conceptual issues:

第一

@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

使用 DropDownListFor 时,第一个参数是提交表单后存储所选值的属性.因此,在您的情况下,您应该将 SelectedOrderId 作为模型的一部分或类似的东西,以便以这种方式使用它:

When using DropDownListFor, the first parameter is the property where your selected value is stored once you submit the form. So, in your case, you should have a SelectedOrderId as part of your model or something like that, in order to use it in this way:

@Html.DropDownListFor(n => n.SelectedOrderId, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

第二,

除了使用 ViewBag 之外,这并没有错,但有更好的方法(将这些信息放在 ViewModel 中),当您的 ViewBag 属性出现小错误"(或未预料到的行为)时,您保存 SelectList 的位置与您放置所选值的属性的名称相同..为避免这种情况,只需在命名包含项目列表的属性时使用另一个名称即可.

Aside from using ViewBag, that is not wrong but there are better ways (put that information in the ViewModel instead), there is a "little bug" (or an unspected behavior) when your ViewBag property, where you are holding the SelectList, is the same name of the property where you put the selected value. To avoid this, just use another name when naming the property holding the list of items.

如果我是你,我会使用一些代码来避免这个问题并编写更好的 MVC 代码:

视图模型:

public class MyViewModel{
   public int SelectedOrderId {get; set;}
   public SelectList OrderTemplates {get; set;}

   // Other properties you need in your view
}

控制器:

public ActionResult MyAction(){
   var model = new MyViewModel();
   model.OrderTemplates = new SelectList(db.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1);
   //Other initialization code

   return View(model);
}

在您的视图中:

@Html.DropDownListFor(n => n.SelectedOrderId, Model.OrderTemplates, "Please select an order template")

这篇关于Html.DropdownListFor 未设置选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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