I'm building my first MVC application, having followed the 'NerdDinner' tutorial through. In creating a dropdownlist from a SelectList in the same way, however, I'm running into an issue.
我正在构建我的第一个MVC应用程序,通过了'NerdDinner'教程。但是,在以相同方式从SelectList创建下拉列表时,我遇到了一个问题。
For some reason, when I bring up the 'Edit' view, the dropdownlist isn't showing the correct selection, even though the data is set otherwise in the database and the 'Details' view shows the correct value. Every one is just coming up with the first value in the list.
出于某种原因,当我调出“编辑”视图时,下拉列表未显示正确的选择,即使数据在数据库中另外设置并且“详细信息”视图显示正确的值。每个人都想出列表中的第一个值。
I've been through the NerdDinner code piece by piece and can't for the life of me see any difference, yet that application will correctly show the dropdown with the current value when editing, and mine doesn't.
我已经逐段浏览了NerdDinner代码并且不能在我的生活中看到任何差异,但是该应用程序将在编辑时正确显示当前值的下拉列表,而我的则没有。
Anyone have a suggestion for where to go from here? I can post code snippets if someone asks for something specific.
有人建议从哪里出发?如果有人要求特定的东西,我可以发布代码片段。
Update:
Within a fieldset:
在字段集中:
<p>
<label for="Parking">Parking Arrangement:</label>
<%= Html.DropDownList("Parking", Model.Parking)%>
<%= Html.ValidationMessage("Parking", "*") %>
</p>
The Edit action:
编辑操作:
//
// GET: /Buyer/Edit/2
public ActionResult Edit(int id)
{
Buyer_Profile buyer_profile = buyerRepository.GetBuyerProfileByID(id);
if (buyer_profile == null)
return View("NotFound");
else if (!buyer_profile.IsOwnedBy(User.Identity.Name, id))
return RedirectToAction("Index", "Home");
else
return View(new BuyerFormViewModel(buyer_profile));
}
In the same way they construct it for the NerdDinner example, I created a '...FormViewModel':
以他们为NerdDinner示例构造它的方式相同,我创建了一个'... FormViewModel':
public class BuyerFormViewModel
{
// Properties
public Buyer_Profile Buyer_Profile { get; private set; }
public SelectList Parking { get; private set; }
// Constructor
public BuyerFormViewModel(Buyer_Profile buyer_profile)
{
Buyer_Profile = buyer_profile;
Parking = new SelectList(BuyerProfileOptions.Parking, Buyer_Profile.Parking);
}
}
And the generated HTML when clicking on 'edit' when a value is already shown in the details view and stored in the d/b:
当值已经显示在详细信息视图中并存储在d / b中时,单击“编辑”时生成的HTML:
<p>
<label for="Parking">Parking Arrangement:</label>
<select id="Parking" name="Parking"><option>No Preference</option>
<option>On Street</option>
<option>Assigned Street</option>
<option>Open Garage</option>
<option>Covered Garage</option>
</select>
</p>
The text fields in the same form have their values populated correctly. It's just all the dropdowns which don't!
相同表单中的文本字段的值已正确填充。这只是所有下拉菜单都没有!
Many thanks for your attention.
非常感谢您的关注。
2 个解决方案
#1
Huh. It appear that htmlhelper is TOO good. I removed the references to the model and everything works!
呵呵。似乎htmlhelper太好了。我删除了对模型的引用,一切正常!
i.e.
<%= Html.DropDownList("Parking", Model.Parking)%>
becomes
<%= Html.DropDownList("Parking")%>
and we're golden. Is it that ViewData contains something called 'Parking' because I'm referencing it in the model so it squashes the other value...or something...?
我们是金色的。 ViewData是否包含一些名为“Parking”的内容,因为我在模型中引用它,因此它会压缩其他值......或者其他东西......?
#2
Quick tip for people - don't name any of your model properties 'Title' when they need a dropdown list. The framework will get mixed up with the View Title and won't work - I spent a full afternoon tearing my hair out over this. Needed to sleep on it to realise what was going on.
人们的快速提示 - 当他们需要下拉列表时,不要将任何模型属性“标题”命名为。该框架将与视图标题混淆,并且无法正常工作 - 我花了整整一个下午的时间撕掉我的头发。需要睡在上面才能意识到发生了什么。
#1
Huh. It appear that htmlhelper is TOO good. I removed the references to the model and everything works!
呵呵。似乎htmlhelper太好了。我删除了对模型的引用,一切正常!
i.e.
<%= Html.DropDownList("Parking", Model.Parking)%>
becomes
<%= Html.DropDownList("Parking")%>
and we're golden. Is it that ViewData contains something called 'Parking' because I'm referencing it in the model so it squashes the other value...or something...?
我们是金色的。 ViewData是否包含一些名为“Parking”的内容,因为我在模型中引用它,因此它会压缩其他值......或者其他东西......?
#2
Quick tip for people - don't name any of your model properties 'Title' when they need a dropdown list. The framework will get mixed up with the View Title and won't work - I spent a full afternoon tearing my hair out over this. Needed to sleep on it to realise what was going on.
人们的快速提示 - 当他们需要下拉列表时,不要将任何模型属性“标题”命名为。该框架将与视图标题混淆,并且无法正常工作 - 我花了整整一个下午的时间撕掉我的头发。需要睡在上面才能意识到发生了什么。