将 html 下拉列表选择的值从 javascript 发送到 mvc 2 中的模型数据库
sending html dropdownlist selected value from javascript to model database in mvc 2
你好,我有一个 html 下拉列表,可以完美地从我的数据库中获取和显示值,现在我有一个 javascript 方法来从列表中获取选定的值,我不知道这个脚本是否甚至可以工作,但无论哪种方式,我如何将选定的值放入我的 controller.cs 代码中?任何输入将不胜感激。
`<h2>Create New Task</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create Task</legend>
<label for="Task">Department</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData["depies"], "Department"), new { onchange = "onSelectedIndexChanged(this)" })%>
<br />`
<h2>Create New Task</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create Task</legend>
<label for="Task">Department</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData[ "depies"], "Department"), new { onchange="onSelectedIndexChanged(this)" })%>
<br />
</div>
<label for="Task">Competency</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData[ "compies"], "Competency1"), new { onchange="onSelectedIndexChanged(this)" })%>
<%-- <%: Html.TextBoxFor(model=>Model.Competency) %>
<%: Html.ValidationMessage( "Name", "*") %>--%>
</div>
<label for="Task">Task</label>
<div class="editor-field">
<%: Html.TextBoxFor(model=>model.Task_Name) %>
<%: Html.ValidationMessage( "Name", "*") %>
</div>
<label for="Task">Description</label>
<div class="editor-field">
<%: Html.TextBoxFor(model=>model.Description) %>
<%: Html.ValidationMessage( "Description", "*") %>
</div>
<input id="Button1" type="submit" value="CreateTask" />
</fieldset>
<% } %>
<script type="text/javascript">
function onSelectedIndexChanged(select) {
var text = select.options[select.selectedIndex].text;
document.getElementById('foo').innerHTML = text;
}
</script>
</asp:Content>
在您使用的脚本中
var text = select.options[select.selectedIndex].text;
最好是select对应下拉文本的id,这样你的问题就解决了。
eg:var text = $("#id_of_the_dropdown").Val();
select id,你可以通过隐藏字段传递id。
你好,我有一个 html 下拉列表,可以完美地从我的数据库中获取和显示值,现在我有一个 javascript 方法来从列表中获取选定的值,我不知道这个脚本是否甚至可以工作,但无论哪种方式,我如何将选定的值放入我的 controller.cs 代码中?任何输入将不胜感激。
`<h2>Create New Task</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create Task</legend>
<label for="Task">Department</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData["depies"], "Department"), new { onchange = "onSelectedIndexChanged(this)" })%>
<br />`
<h2>Create New Task</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create Task</legend>
<label for="Task">Department</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData[ "depies"], "Department"), new { onchange="onSelectedIndexChanged(this)" })%>
<br />
</div>
<label for="Task">Competency</label>
<div class="editor-field">
<%: Html.DropDownList( "lsttasks", new SelectList((IEnumerable)ViewData[ "compies"], "Competency1"), new { onchange="onSelectedIndexChanged(this)" })%>
<%-- <%: Html.TextBoxFor(model=>Model.Competency) %>
<%: Html.ValidationMessage( "Name", "*") %>--%>
</div>
<label for="Task">Task</label>
<div class="editor-field">
<%: Html.TextBoxFor(model=>model.Task_Name) %>
<%: Html.ValidationMessage( "Name", "*") %>
</div>
<label for="Task">Description</label>
<div class="editor-field">
<%: Html.TextBoxFor(model=>model.Description) %>
<%: Html.ValidationMessage( "Description", "*") %>
</div>
<input id="Button1" type="submit" value="CreateTask" />
</fieldset>
<% } %>
<script type="text/javascript">
function onSelectedIndexChanged(select) {
var text = select.options[select.selectedIndex].text;
document.getElementById('foo').innerHTML = text;
}
</script>
</asp:Content>
在您使用的脚本中 var text = select.options[select.selectedIndex].text; 最好是select对应下拉文本的id,这样你的问题就解决了。 eg:var text = $("#id_of_the_dropdown").Val(); select id,你可以通过隐藏字段传递id。