【问题标题】:Trying to pass a variable to Jquery from Razor试图从 Razor 将变量传递给 Jquery
【发布时间】:2016-08-31 03:07:01
【问题描述】:

所以我正在 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>

我添加了几张图片来帮助说明我想要做什么。

【问题讨论】:

  • 当用户添加另一个下拉列表时,新的下拉列表是否与第一次填充的下拉列表具有相同的选项?
  • 嗨,马克,应该是的,但目前它只是空白。

标签: javascript c# jquery asp.net razor


【解决方案1】:

它的发生是因为 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>

并在控制器中添加局部视图

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
}

在此之后,您可以通过单击添加按钮将下拉列表添加到您的 html 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 2015-10-30
    • 1970-01-01
    • 2012-02-15
    • 2018-12-14
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多