[英]MVC: How can I make sure that my field have the value selected inside my view on the edit form?
I am using MVC3-Viewmodel on my project. 我在我的项目上使用MVC3-Viewmodel。
I have a create and edit form, What I am trying to do is to make sure that after a user Create a form, he/she should be able to edit it, when the user is at the edit page I want the values to be displayed in the inputs and selects. 我有一个创建和编辑表单,我要做的是确保用户创建表单后,他/她应该能够对其进行编辑,当用户位于编辑页面时,我希望这些值是显示在输入和选择中。 The only field that is not working is my DropDownList.
唯一不起作用的字段是我的DropDownList。 I have no idea why.
我不知道为什么。
If I close the application and go to the edit form I want that the DropDownList have the value that was created. 如果我关闭该应用程序并转到编辑表单,则希望DropDownList具有已创建的值。
here is the code: 这是代码:
foreach (SelectedQuestionViewModel items in Model.AllSelectedQuestions)
{
<select id="selectstyle2" class="Grade">
@{
<option>n/a</option>
int Grade = 1;
for (int i = 1; i <= 10; i++)
{
<option @(items.Grade.HasValue && items.Grade.Value == Grade ? "selected" : "") >@(i)</option>
}
}
</select>
}
any help kind of help is appreciated. 任何帮助类型的帮助表示赞赏。
note: I use a ajax post to my controller, inside the controller I fill my "Model.Grade" with the value that was selected from this DDL. 注意:我在控制器中使用ajax发布,在控制器内部,我使用从此DDL中选择的值填充“ Model.Grade”。
You have your select tag within your foreach loop. 您可以在 foreach循环中使用select标记。 Write this tag outside, like this:
像这样在外面写这个标签:
<select>
@foreach(var x in Model)
//write out options
</select>
also have a look at Html.DropDownList extension method. 还可以看看Html.DropDownList扩展方法。 This does what you're trying to do by hand.
这就是您要手动执行的操作。
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx http://msdn.microsoft.com/zh-CN/library/system.web.mvc.html.selectextensions.dropdownlist.aspx
as an example 举个例子
@{
var selectListItems = (new[] { 1,2,3}).Select(i=>new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
@Html.DropDownListFor(m=>m.AllSelectedQuestions[0].Grade, selectListItems, "n/a")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.