如何在MVC3 DropDownList中获取选定值 [英] How to fetch selected value from a dropdownlist in MVC3
问题描述
这听起来可能很简单,但我在一个非常奇怪的问题卡住了。
This may sound simple but i am stuck up at a very strange problem.
我已经创建了我的应用程序下拉菜单的,我不能够获取所选值上编辑。
I have created dropdown's in my application and i am not able to fetch the selected value On Edit.
下面是我的看法:
<div class="editor-field">
@Html.DropDownListFor(model => model.Hobbyhome.HobbyHomeAddressList.First().ProvincialState.ProvincialStateID, new SelectList(ViewBag.State, "ProvincialStateID", "ProvincialStateName"), "-----Select State-----", new { @id = "State" })<font color="red">*</font>
</div>
我已经写在控制器的函数来获取价值,我还能够获取价值,但是当它在查看它亘古不变的显示我的价值结果
我已经创造了另一个下拉列表如下:结果
i have written a function in controller to fetch the value and i am also able to fetch the value but when it goes in View it doesnot show me the value
i have created another dropdown like this:
@Html.DropDownListFor(model => model.Hobbydetail.Hobbymaster.HobbyId, new SelectList(ViewBag.Hobby, "HobbyId", "HobbyName"), "----Select Hobby----", new { @id = "Hobby" })<font color="red">*</font>
和奇怪的是,我可以看到第二个下拉为第壹的价值,但不
结果
我想也许是因为我在第一个下拉使用 list.First()并因此isgiving bcoz在编辑视图页这个问题,只能说明我-----选择述明─ - 作为第一个下拉列表值。
请帮我
And the strange thing is i can see the value of the second dropdown but not for first One
What i think is maybe because i am using an list.First() in the first dropdown and so it isgiving this problem bcoz On edit view page it only shows me "-----Select State--" as value for first dropdown.
Please Help me
推荐答案
要取得选择的值对象,在你编辑观点:
To get the "selected value object" in you edit view :
@Html.DropDownListFor(model => model.Hobbydetail.Hobbymaster.HobbyId,
new SelectList(ViewBag.Hobby, "HobbyId", "HobbyName", Model.Hobbydetail.Hobbymaster.HobbyId),
"----Select Hobby----", new { @id = "Hobby" })
要在你的下拉列表中所有的元素,请确保您的ViewBag.Hobby正显示出编辑视图之前初始化并填写您的控制器!
To have all elements in your DropDown, be sure that your ViewBag.Hobby is initialized and filled in your controller before showing Edit view!
这篇关于如何在MVC3 DropDownList中获取选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!