简体   繁体   English

MVC:如何确保我的字段在编辑表单的视图内选择了值?

[英]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.

相关问题 我怎样才能使我选择的表格在表格中表现突出? - How can I make my selected outstanding in the form? 如何检查以确保我所有的input:radio元素都具有值? - How do I check to make sure all my input:radio elements have a value? 当我有一个隐藏的字段(如果选择了一个选项时会显示)时,我无法获得select元素的值 - I can't get the value of my select element when i have a hidden field that is revealed if an option is selected 我如何确保我的变量在setInterval内部不变 - How can I make sure that my variable does not change inside the setInterval 我希望使我的代码验证一个字段,以确保它是四位数。 我怎样才能做到这一点? - I wish to make my code validate a field to make sure it is a number of four digits. How can I do this? 我正在做一个打字稿项目,我试图让我的编辑按钮“编辑”我选择的行。 但我不知道该怎么做 - I am doing a typescript project and I am trying to make my edit button to “edit” the row that I have selected. But I have no idea on how to do it 如何确保我的值被添加并存在,然后运行另一个代码jquery? - How can I make sure my value was add and exist and then run another code jquery? 我如何使我的JavaScript读取表单值? - how can i make my javascript to read the form value? 如何确保在表单内选择了所有文本字段和单选按钮? - How do i make sure ALL textfields & Radio Buttons are selected inside a form? 如何使我的 td 中的所有输入对于单击编辑按钮的行都是可编辑的 - How can I make all the inputs inside my td's editable for the row the edit button was clicked on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM