[英]Trying to pass a variable to Jquery from Razor
因此,我正在ASP.net中的一个项目上工作,并且遇到一种情况,即我已经创建了一个表单,该表单向用户提供了一个下拉菜单供您选择。 最重要的是,下面的Jquery允许用户添加其他下拉字段。 这可行。 现在我的问题来自于第一个下拉列表,其中列出了一些机构,这些机构可以很好地工作,因为它是从html形式的C#中填充的。
当用户选择添加另一个下拉菜单时,该框为空。 我尝试将C#直接添加到Jquery中,但这不起作用。
我对项目都使用ASP.net或MVC并没有过多的经验,将值传递到Jquery中以便将列表添加到下拉列表的最佳方法是什么?
这是下面的代码
<script>
$(document).ready(function () {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function (e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
x++; //text box increment
$(wrapper).append('<div><select style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" /></select><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
这是HTML
<div class="col-md-3 BasicInfoFormLabelColumn">
<label for="restrictedInstitute" class="formLabel">Restricted Institutes: (Can't Apply)</label>
</div>
<div class="col-md-3 input_fields_wrap">
<select style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" id="selectRestricted" />
<option value="0">Please Select</option>
@foreach (var item in Model.institute)
{
if (@item.TradingName != null && @item.TradingName != " " && @item.TradingName != "")
{
<option value="@item.TradingName">@item.TradingName</option>
}
}
</select>
<div class="row">
<div class="col-md-12 BasicInfoFormRow ">
<button class="add_field_button addMore">+Add More institutes</button>
</div>
</div>
</div>
我添加了一些图像,以帮助弄清我要做什么。
之所以会这样,是因为U只在您的Jquery内部附加select
而没有其option
。 我建议使用AJAX和局部视图来实现您的目标。 首先,将dropdownlist的呈现方式分成另一个cshtml,例如,名称为Institute.cshtml
Institute.cshtml
@model xxxx.Model.Institute //model would be your institute
<select style="padding-left: 5px; width: 100%;" class="BasicInfoFormControl" onblur="" name="restrictedInstitute[]" id="selectRestricted" />
<option value="0">Please Select</option>
@foreach (var item in Model)
{
if (@item.TradingName != null && @item.TradingName != " " && @item.TradingName != "")
{
<option value="@item.TradingName">@item.TradingName</option>
}
}
</select>
然后在HTML中将其称为“部分视图”
<div class="col-md-3 input_fields_wrap">
@Html.Partial("Institute", Model.Institute)
<div class="row">
<div class="col-md-12 BasicInfoFormRow ">
<button class="add_field_button addMore">+Add More institutes</button>
</div>
</div>
</div>
并在你的控制器中添加partialview
xx控制器
PartialViewResult Institute()
{
return PartialView();
}
这是将您的下拉列表分成另一个partialView的第一步。第二步是在您的“添加单击”按钮中调用一个ajax
,然后使用此partialView
来获取结果
javascript
$(document).ready(function () {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function{ //on add input button click
if (x < max_fields) { //max input box allowed
x++; //text box increment
$.ajax({
url: '@Url.Action("AddDropdown", "YourController")',
type: "GET",
success: function (result) {
$(wrapper).append(result);
}
});
}
});
$(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
该Javascript
意味着,当您单击“添加”按钮时,您将使用Ajax向您的控制器请求。 然后,控制器将帮助您呈现下拉列表并将结果发送回客户端。 因此,您需要在控制器中添加另一个功能以接收此Ajax请求
xx控制器
public ActionResult AddDropdown()
{
Model.Institute Result = new Model.Institute()
//you can generate your Model.Institue in here
Return PartialView("Institute", Result); //this will render Result with Institute.cshtml
}
之后,您可以点击添加按钮将dropdownlist添加到html中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.