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

查看:111
本文介绍了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?

我已经更新了我的code,因为有在功能上略有变化但似乎仍然遇到此问题。这就是我的观点:

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 选项的编号,我想选,我也跟的文字试图选项但也不能正常工作。

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.

任何想法?

推荐答案

您code有一些概念上的问题:

Your code have 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 this way:

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

除了使用ViewBag的,这是没有错的,但有更好的方法(把这些信息在视图模型代替), 有一个小错误,当你ViewBag财产,你在哪里举办的SelectList,就是你把选定值的属性的名称相同。为了避免这种情况,命名的属性,物品的列表时,只需要使用另一个名字。

Aside of using ViewBag, that is not wrong but there are better ways (put that information in the ViewModel instead), there is a little bug 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.

有些code,如果我是你,我会用它来避免这个问题,并写出更好的MVC code:

视图模型:

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天全站免登陆