我有这个代码,如何获取@htmlDropDownList的选定值以将此值放入我的@ Url.Action而不是 1 ,我已阅读使用javascript o jquery但我不能弄清楚我如何直接在视图中执行此操作,这对我的
无效var idempresa = $('IdEmpresa').val()
<a href="@Url.Action("List","Script")?idempresa=**idempresa**"
代码:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Script</legend>
<div class="editor-label">
@Html.LabelFor(model => model.IdEmpresa, "Empresa")
</div>
<div class="editor-field">
@Html.DropDownList("IdEmpresa", String.Empty )
@Html.ValidationMessageFor(model => model.IdEmpresa)
</div>
</fieldset>
}
href="@Url.Action("List","Script")?idempresa=**1**"
概率pd。谢谢你的帮助
编辑:
@using System.Web.Mvc.Html
@model SistemaDispositivos.Models.Script
@{
ViewBag.Title = "Script";
}
<h2>Script</h2>
<script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Script</legend>
<div class="editor-label">
@Html.LabelFor(model => model.IdEmpresa, "Empresa")
</div>
<div class="editor-field">
@Html.DropDownList("IdEmpresa", String.Empty)
@Html.ValidationMessageFor(model => model.IdEmpresa)
</div>
</fieldset>
}
<script type="text/javascript" language="javascript">
$(document).ready(function () {
var url1 = '@Url.Action("List","Script")?idempresa=';
$('IdEmpresa').change(function() {
$urlId = $('urlid');
$urlId.attr('href', url + $(this).val());
});
});
</script>
<a id="urlid" href="@Url.Action("List","Script")?idempresa=url1"> </a>
我缺少什么?
答案 0 :(得分:2)
现在好了,这就是我想要的感谢帮助:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Script</legend>
<div class="editor-label">
@Html.LabelFor(model => model.IdEmpresa, "Empresa")
</div>
<div class="editor-field">
@Html.DropDownList("IdEmpresa", string.Empty )
@Html.ValidationMessageFor(model => model.IdEmpresa)
</div>
</fieldset>
}
<script type="text/javascript" language="javascript">
function valid() {
var listaScriptUrl = '@Url.Action("List", "Script", new { idempresa = 0})';
var input = $('#IdEmpresa').val();
$('#Lista-Script').load(listaScriptUrl.replace('0', input));
}
</script>
<input type="button" value="Click Here" onclick="valid()">
<div id="Lista-Script">
</div>
所以现在在div Lista-Script中,当我点击按钮时,我得到了我想要的视图
答案 1 :(得分:0)
视图将呈现HTML,因此您无法获取下拉列表的选定值并将其放回已在服务器端执行的视图的变量。您可以做的是在标记上放置一个id并更新下拉列表更改事件的href。
$(document).ready(function(){
$('IdEmpresa').change(function(){
var val = $(this).val();
var newurl = $("urlid").attr("href") + val;
$("urlid").attr("href",newurl);
});
});
<a id="urlid" href="@Url.Action("List","Script")?idempresa="> </a>
答案 2 :(得分:0)
我会像Bipins发布的那样接近这个,但有一个主要的例外。
<fieldset>
<legend>Script</legend>
<div class="editor-label">
@Html.LabelFor(model => model.IdEmpresa, "Empresa")
</div>
<div class="editor-field">
@Html.DropDownList("IdEmpresa", *This needs to be a list of ListItems*, new { id = "idEmpresa" })
@Html.ValidationMessageFor(model => model.IdEmpresa)
</div>
</fieldset>
$(document).ready(function()
{
var url = '@Url.Action("List","Script")?idempresa=';
$('#idEmpresa').change(function()
{
$urlId = $('urlid');
$urlId.attr('href', $urlId.attr('href') + $('this').val());
}
});
通过在js变量中缓存原始URL,您总是可以附加一个干净的平板。